Use [embed] filter in template files

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?

2 s
2

Use wp_oembed_get( $url ) instead. Make sure you echo it in your template file. So, something like this:

<?php
// tot necessary to set this but good if $url is coming from a function
$url="https://www.youtube.com/watch?v=jofNR_WkoCE";

// echo the function in your template to render the video
echo wp_oembed_get( $url );
?>

Leave a Comment