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

I encountered a problem...

There is content that contains a YouTube link

Lorem ipsum bla bla bla http://www.youtube.com/watch?v=nTDNLUzjkpg

My plugin display this content with that code in the view:

<?php echo apply_filters( 'the_content', $data['meta']->start_page_content ); ?>

The problem is that if you open a page with usual GET method via the browser - the link becomes a YouTube player, but when i loaded the same template via AJAX - it seems that apply_filters does not work - at the output I get just a link, not a YouTube player code.

How can i fix it?

share|improve this question

As you haven't posted, what exactly you're doing with AJAX, we can just guess. And I guess, that you're doing it extremly wrong - no need to "hook/filter" in ajax.

AJAX is for admin(?)

Basically, AJAX stuff is meant to be admin stuff. This means, that you got a wp_ajax_ hook and a wp_ajax_nopriv_ hook, but is_admin() will still return true for all requests. That's something that is left over, or just tradition.

If you want to add something via AJAX, than simply do it with javascript.

Use wp_localize_script() to transport data from PHP » javascript

First localize your meta data to make it usable in the script:

wp_enqueue_script( 'my-script-handle', etc.... );
        'youtubevideo' => $data['meta']->start_page_content

Simple jQuery does it all

Then add it to the container, or wherever you need it:

// Depending on what exactly your "video" is (embed/link/etc.), you need to adjust the var.
var youtube_video = my_localized_data.youtubevideo;
jQuery( '#content' ).append( youtube_video );
share|improve this answer
my problem is that the function apply_filters does not work properly, not in AJAX call – user16962 Jul 9 '12 at 11:32
@user16962 Filters don't work in AJAX calls... as I described and explained above ↑ : »(...)no need to "hook/filter" in ajax.« – kaiser Jul 9 '12 at 11:39
okay... do you know the WP function, that converts standard EMBED links to their container code? – user16962 Jul 9 '12 at 11:44
Search on Codex? – kaiser Jul 9 '12 at 11:51
problem fixed! <?php global $wp_embed; $wp_embed->post_ID = $data['post_ID']; ?> <?php echo $wp_embed->run_shortcode( $data['taster_meta']->start_page_content ); ?> – user16962 Jul 9 '12 at 12:20

The oEmbed is not processed because of the lack of $post->ID (as global). You can just do in your function :

global $post; $post->ID = PHP_INT_MAX;

...and then the apply_filter( 'content' ...) will just work as if this was not an Ajax call.

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.