WordPress automatically embeds a youtube video’s if I use:
http://www.youtube.com/watch?v=Xog1T5dUxcw [/embed ]
</code></pre>
<p>This is great, but it doesn't work if I use it in a template file. I have a custom field where the admin can put a URL to a YouTube video. I want to get the video in the single-post using the following code:</p>
<pre><code><?php
$custom = get_post_custom($post->ID);
$url = $custom['_videoLink'][0];
?>
<div class="video">
<?php $url; ?>
</div>
How can I convert the Youtube URL into an embed URL using the standard WordPress function?
Use
wp_oembed_get( $url )
instead. Make sure youecho
it in your template file. So, something like this:Normally you have to use do_shortcode in a template to place a shortcode outside of the content, however, I’ve had trouble with the embed shortcode specifically and could not make it work that way. I found this solution which works, but maybe there’s a way to do this with do_shortcode and I’ve missed something.