Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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.... );
wp_localize_script(
     'my-script-handle'
    ,'my_localized_data'
    ,array(
        '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
1  
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

Your Answer

 
discard

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.