Skip Navigation

[Resolved] AJAX update on parametric search not running shortcodes

This thread is resolved. Here is a description of the problem and solution.

Problem:

How can I get my filter(do_shortcode_tag) to run on AJAX updates as well as full page loads?
https://toolset.com/forums/topic/ajax-update-on-parametric-search-not-running-shortcodes/#post-905159

Solution:

It is a custom codes problem, see the fix of client:
https://toolset.com/forums/topic/ajax-update-on-parametric-search-not-running-shortcodes/#post-906988

Relevant Documentation:

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by jennyleeS 6 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#905159

I am trying to modify the display of the views-filter checkboxes used in a parametric search. I've written a WordPress 'do_shortcode_tag' filter for the 'wpv-control-post-taxonomy' shortcode. It works on initial load and full page refresh. When the results are updated via AJAX -- when an input is changed (checkbox checked/unchecked) -- the new results are not run through the filter.

How can I get my filter to run on AJAX updates as well as full page loads?

add_filter( 'do_shortcode_tag', 'bcaccs_add_checkbox_classes', 99, 4);

function bcaccs_add_checkbox_classes( $content, $tag, $attr, $m ) {
  file_put_contents('shortcodes.txt', date('r') . " $tag\n", FILE_APPEND);
  if ($tag == 'wpv-control-post-taxonomy' && $content != '') {
    if ($attr['taxonomy'] == 'topic' && $attr['type'] == 'checkboxes' && $attr['url_param'] == 'wpv-topic') {
      // Do stuff
    }
  }
  
  return $content;
}
#905309

Hello,

It is a custom codes problem, I have tried your PHP codes in my localhost it works fine, you can not output result in the custom PHP function, but you can change the result, for example:

add_filter( 'do_shortcode_tag', 'bcaccs_add_checkbox_classes', 99, 4);
 
function bcaccs_add_checkbox_classes( $content, $tag, $attr, $m ) {
  file_put_contents('shortcodes.txt', date('r') . " $tag\n", FILE_APPEND);
  if ($tag == 'wpv-control-post-taxonomy' && $content != '') {
    if ($attr['taxonomy'] == 'topic' && $attr['type'] == 'checkboxes' && $attr['url_param'] == 'wpv-topic') {
      // Do stuff
        $content .= 'Append some test text here ... ';
    }
  }
   
  return $content;
}

And if you need to add some CSS class to the taxonomy checkboxes field, you can try to add attribute "class" to the shortcode [wpv-control-post-taxonomy], for example:
[wpv-control-post-taxonomy class="my-class" ...]

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-control-post-taxonomy
class (opt):
'class names separated by space'

#905690

You have missed my question.

  • I have working code
    (the code snippet was intended to make the filter clear, and is not the actual code)
  • The code is executed correctly on a full page load
  • The code is NOT executed when the filter checkboxes are updated via AJAX
    (For example: When a user clicks a filter checkbox and the view results are updated)

.

How can I make the search view execute my code for both a standard page load and a AJAX update load?

I am using a hierarchical taxonomy and need to assign parent/child classes to the term checkboxes based on their relationship. I do not see a short code option that does this.

#905961

As I mentioned above, I have tested your PHP codes in my localhost, it works fine:
the search view execute your code for both a standard page load and a AJAX update load

With below steps:
1) Modify the PHP codes as below:
https://toolset.com/forums/topic/ajax-update-on-parametric-search-not-running-shortcodes/#post-905309

2) Test the view with AJAX search form, I can see the text after change the "topic" filter checkboxes:

Append some test text here ... 

If you still need assistance for it, please provide a test site with the same problem, fill below private detail box with login details and FTP access, also point out the problem page URL, and where I can edit you PHP codes, I can setup a demo for you.

#906988

OK, I see that the filter is being run during an AJAX call.

I was using file_put_contents() to write a debug file and tailing the file to troubleshoot. Regular page loads output the file to the WordPress root directory. AJAX page calls output the file to '/wp-admin'. That's why I wasn't seeing results and thinking the filters hadn't ran.

My code wasn't working because I'm using get_query_var( 'wpv-topic' ) to get information about page state. This does not exist on the AJAX call.

I also found in my research that WordPress considers the do_shortcode_tag filter private and it should not be used by outside of core. I need to rethink my approach.

Thanks for your help.