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;
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)
When WordPress displays post content, it’s not running
do_shortcode()
on the post content, it’s runningapply_filters( 'the_content', $content )
. Shortcode filters are applied onthe_content
filter, which is why you have to add extra filters to get them to work in widgets or the footer or elsewhere.Don’t know if you worked around a solution, but i had a similar problem and solved by:
binding my own gallery function to the original gallery shortcode (instead of creating a new shortcode for a custom gallery function)
More details here.
Awww…
in admin-ajax.php:
in cleaner-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!