WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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;


$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)

share|improve this question

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.

share|improve this answer
Thanks, but when including an Ajax request I guess that do_shortcode() is required to render something more than a raw bracketed shortcode output. According to your suggestion, I've added apply_filters('the_content',$content); but it leads to the same result. Should I do something else? However, it seems that the post_gallery filter hook is applied, but the Cleaner Gallery filter is not added to it. – 3dolab Jun 29 '11 at 17:09
I can't add again this filter to process the content retrieved through Ajax since it should apply only on the shortcode part... – 3dolab Jun 29 '11 at 17:17
Just to be clearer I must point out that the ordinary [gallery] shortcode do work in both normal and ajax pages. What I'm trying to achieve is to get it filtered since the function gallery_shortcode($attr) runs $output = apply_filters('post_gallery', '', $attr); if ( $output != '' ) return $output; to "Allow plugins/themes to override the default gallery template"; when a custom hacked plugin shortcode replaces/overrides the default shortcode, it fails and it looks like the plugin is invisible to the Ajax handler. (Ah, good ol' separate PHP file!) – 3dolab Jun 29 '11 at 19:34

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.

share|improve this answer
Celso, this is not Twitter, you can use full links instead of shortened ones, thanks! +1 for the simple and fast solution. – brasofilo Oct 20 '12 at 4:06
up vote 0 down vote accepted

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!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.