admin管理员组

文章数量:1422330

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

[embed]  [/embed ]

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:

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

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

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

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

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:

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

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

Share Improve this question edited May 5, 2018 at 6:02 CommunityBot 1 asked Aug 19, 2011 at 15:55 Sjoerd BoerrigterSjoerd Boerrigter 1692 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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/watch?v=jofNR_WkoCE';

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

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;
?>

本文标签: Use embed filter in template files