Skip Navigation

[Closed] Add a link when a post is not available in the dropdown relationship field

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by Christopher Amirian 2 weeks, 1 day ago.

Assisted by: Christopher Amirian.

Author
Posts
#2820082

Hi

I have a form with a relationship dropdown field to select a post.
If the result is "No results found" I'd like to have a "add" button next to the field in order to open a new window and add the missing post.
Is there a "native code option" to do that ? Or I need to detect that "no result" sentence with JS to display a "add" button ?

#2820146

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. There’s no built-in “show an Add button when the relationship dropdown says ‘No results found’ ” option in Toolset.

This will need custom code, which is outside of our support scope. One thing that might work is that you change the No result option with some sort of button that you can control via JS. Because the relationship selector is Select2, you can watch its events and append a button when the “no results” message appears. Example:

jQuery(function($){
  var $sel = $('select.cred-relationship-role-child'); // adjust selector

  $sel.on('select2:open', function(){
    setTimeout(function(){
      var $drop = $('.select2-container--open .select2-results');
      var $msg  = $drop.find('.select2-results__message');
      if ($msg.length && !$drop.find('.js-add-related').length) {
        $('<a class="js-add-related" target="_blank" href="/add-company/?return=' +
          encodeURIComponent(location.href) + '">+ Add new Company</a>')
        .css({display:'block', padding:'8px'})
        .appendTo($drop);
      }
    }, 0);
  });
});

This isn’t an official Toolset setting; it’s just JS on top of the existing control.

Thanks.

The topic ‘[Closed] Add a link when a post is not available in the dropdown relationship field’ is closed to new replies.