I have a page that is calling in the content from other posts (a custom post type, specifically) and inserting it into the page. It all works great, but I’m losing the functionality of WordPress taking youtube (and other embeddable links) and automatically inserting the embedded video.

I’ve tried apply_filters( 'the_content', $my_content );, but I think that filter isn’t even available via admin-ajax.php.

I of course already call do_shortcode( $my_content ), but what can I use, in similar fashion, to get the embeddable links to embed?

UPDATE:

In response to bonger’s answer, I’ve updated the code to this:

wpautop( do_shortcode( $wp_embed->run_shortcode( $my_content ) ) )

($wp_embed was globalized before this line of code)

Note that the wpautop() and do_shortcode() are totally irrelevant to this issue, but I put them in here just for completeness. This code is outputting the youtube url as a link to the youtube video, but not embedding the video in the page.

3 Answers
3

I think the solution is amazingly simple here:

You’re just missing a single line of code in your ajax callback, namely:

global $post;

where I assume you’re using:

$post = get_post( $post_id );

The reason is that there’s a global post check in the WP_Embed::shortcode() method.

More details in my answer here.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *