Ajax, filters and shortcodes

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, as recommended in the famous 5 tips.

Read More

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.

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.
Please help me!

(also posted on the WP.org forums)

Related posts

Leave a Reply

3 comments

  1. When WordPress displays post content, it’s not running do_shortcode() on the post content, it’s running apply_filters( 'the_content', $content ). Shortcode filters are applied on the_content filter, which is why you have to add extra filters to get them to work in widgets or the footer or elsewhere.

  2. Don’t know if you worked around a solution, but i had a similar problem and solved by:

    • unbinding the original gallery function from the gallery shortcode
    • binding my own gallery function to the original gallery shortcode (instead of creating a new shortcode for a custom gallery function)

      //unbinds WordPress function in order to replace it with new code
      remove_shortcode('gallery', 'gallery_shortcode');
      //binds own custom gallery code function
      add_shortcode('gallery', 'acmecustomgallery_shortcode');
      

    More details here.

  3. Awww…
    in admin-ajax.php:

    define('WP_ADMIN', true);
    

    in cleaner-gallery.php:

    function cleaner_gallery_setup() {    
        if ( is_admin() ) {
            require_once( CLEANER_GALLERY_DIR . 'admin.php' );
        } else {
            require_once( CLEANER_GALLERY_DIR . 'gallery.php' );
        }
    }
    

    So I have to manually include & run the necessary plugin code skipped after checking the WP_ADMIN constant, but I guess the need for a frontend ajax handler is becoming increasingly urgent!