Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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

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

share|improve this question

2 Answers 2

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.

<?php
$custom = get_post_custom($post->ID);
$url = $custom['_videoLink'][0];
if($url):
    $shortcode = '[embed]'.$url.'[/embed]';
    global $wp_embed;
    echo $wp_embed->run_shortcode($shortcode);
endif;
?>
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
1  
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

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

<?php
$your_YouTube_url = 'http://youtube.com/JaNH56Vpg-A';
echo wp_oembed_get( $your_YouTube_url );
?>
share|improve this answer

Your Answer

 
discard

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.