Search code examples
phpwordpressyoutubeembedvimeo

How to get the first embed content of a post


I'm trying to get the first embed url from the post. I know wordpress auto-embeds them but can't find a method to just get the embeds.

                 $urls = array();
                 $p = get_post();
                 preg_match('@https?://(www.)?(youtube|vimeo)\.com/(watch\?v=)?([a-zA-Z0-9]+)@im', $p->post_content, $urls);
                 echo wp_oembed_get( $urls[0] );

Solution

  • Since Wordpress 2.9, wp_oembed_get function has been included.

    Example:

    <?php $embed_code = wp_oembed_get($url); ?>
    

    Here is the link to the function, to get the embeds:

    http://codex.wordpress.org/Function_Reference/wp_oembed_get