WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

WordPress autmatically embeds a youtube video's if I use:

[embed] http://www.youtube.com/watch?v=Xog1T5dUxcw [/embed ]

This is great, but it doesn't work if I use it in an template file. I have a custom field where the admin can put a URL to a YouTube video. I want get the video in the sinle-post using the following code:

  $custom = get_post_custom($post->ID);
  $url = $custom['_videoLink'][0];
<div class="video">
  [embed]<?php $url; ?>[/embed]

How can i convert the Youtube URL into an embed URL using the standard WordPress [embed] function?

share|improve this question
up vote 4 down vote accepted

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

$your_YouTube_url = 'http://youtube.com/JaNH56Vpg-A';
echo wp_oembed_get( $your_YouTube_url );
share|improve this answer
Easy and best solution for this problem. +1 – Sam Sep 3 '14 at 8:04

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.

$custom = get_post_custom($post->ID);
$url = $custom['_videoLink'][0];
    $shortcode = '[embed]'.$url.'[/embed]';
    global $wp_embed;
    echo $wp_embed->run_shortcode($shortcode);
share|improve this answer
Works like a charm. Thanks for the help! – Sjoerd Boerrigter Aug 22 '11 at 8:58
You can do also echo do_shortcode($shortcode) – keatch Jan 30 '12 at 8:20
@keatch - have you tried this specifically with WP's embed shortcode? as I mentioned, that specific one would not work for me, whereas other short codes normally do. I haven't done much investigation as to why. This was also not with the most recent WP version. – Milo Jan 30 '12 at 15:20
I know this is old, but just to point out I'm still having the same issue now in 2013 with the embed shortcode not working in template files. The above solution worked great though. – Eckstein Aug 15 '13 at 6:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.