Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use WP oembed script outside the_content

I've a custom post type "video" and wanted to show videos like youtube, dailymotion in a specified area using WP default oembed script. So i'm using a custom field"video url", but the problem is that oembed work in the_content not with custom field. so how can i do this. or any other solution

like image 212
Stephen Finn Avatar asked Feb 18 '13 05:02

Stephen Finn


1 Answers

If the custom field only contains the video URL like http://www.youtube.com/watch?v=dQw4w9WgXcQ, then you can get the oEmbed HTML code with wp_oembed_get:

$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;

If your custom field contains more than just the URL, you could use the the_content filter to do the same thing the the_content-function does:

$content = "<h2>this video is great</h2>\n<p>check it out</p>\n"
  . "[embed]http://www.youtube.com/watch?v=dQw4w9WgXcQ[/embed]";

$htmlcode = apply_filters('the_content', $content);
echo $htmlcode;
like image 145
vstm Avatar answered Sep 28 '22 07:09

vstm