WordPress.org

Forums

shortcodes, filters and ajax (4 posts)

  1. 3dolab
    Member
    Posted 3 years ago #

    Could you figure out why I'm unable to get shortcode filters applied in ajax inclusion of posts?

    Let me explain better: I've managed to include a post from within another post, through admin-ajax.php.
    Obviously I don't want to display the shortcode tags, nor I'd like to strip them, so I'm echoing do_shortcode($post->post_content)

    At this point, the post gallery gets rendered but unfiltered, even though I'm running the "Cleaner Gallery" plugin, which starts with add_filter( 'post_gallery', 'cleaner_gallery', 10, 2 ); and succesfully works in normal (non-ajax) posts.

    I've been trying to debug this issue by replacing

    $output = apply_filters('post_gallery', '', $attr);
    	if ( $output != '' )
    		return $output;

    with

    $output = apply_filters('post_gallery', '', $attr);
    echo 'FILTERED OUTPUT = '.$output.' !';
    	if ( $output != '' )
    		return $output;

    inside the function gallery_shortcode($attr) located in wp-includes/media.php.
    It seems that it runs twice, but in ajax inclusions the output is empty at first time and then it's not filtered.

    Please help me!

  2. 3dolab
    Member
    Posted 3 years ago #

    Well, in all effect it happens that also the "2nd round" echoed output I mentioned remains empty when setting again add_filter before do_shortcode; otherwise it's shown (but, as I said, unfiltered).

    I've also tried to track it with

    echo has_filter('post_gallery');
    echo current_filter();

    and I've discovered that has returns always '1', while current first returns 'post-gallery' as expected, and then it gets overwritten by the 'wp_ajax_nopriv_etc' hook, which is required to handle requests with admin-ajax.php but it's actually an action and not a filter. (?!?)

    I've also tried without success to hack the cleaner gallery plugin, in order to make it directly run the shortcode without filtering the default function, removed the originary shortcode and added that new one.
    It works like a charm for normal posts, but it seems there is no way to implement the plugin in ajax requests.

    I'm getting more and more confused.

  3. 3dolab
    Member
    Posted 3 years ago #

    OK, I've got it:
    the Cleaner Gallery plugin first checks against is_admin() in function cleaner_gallery_setup(), so it is required to manually run the code contained in the else statement, since admin-ajax.php defines the WP_ADMIN constant as true.

  4. Corey McKrill
    Member
    Posted 3 years ago #

    Thanks for pointing me in the right direction! I had the same problem with the Cart66 plugin: the script only initiates shortcodes if is_admin() returns false, and anything going through admin-ajax.php will return true. I ended up manually loading the shortcodes by adding Cart66's shortcode handler to the beginning of my ajax function: Cart66::initShortcodes();

Topic Closed

This topic has been closed to new replies.

About this Topic