admin管理员组

文章数量:1426007

echo do_shortcode('');

Is merely printing

" 

out onto the page.

I know I can display the video with html with

<iframe src="" width="560" height="315" frameborder="0" allowfullscreen></iframe>

but I'm trying to leverage wordpress' built-in methods. How do I do this?

echo do_shortcode('https://www.youtube/watch?v=vZBCTc9zHtI');

Is merely printing

https://www.youtube/watch?v=vZBCTc9zHtI" 

out onto the page.

I know I can display the video with html with

<iframe src="http://www.youtube/embed/vZBCTc9zHtI" width="560" height="315" frameborder="0" allowfullscreen></iframe>

but I'm trying to leverage wordpress' built-in methods. How do I do this?

Share Improve this question edited Sep 22, 2016 at 4:00 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Sep 13, 2016 at 17:33 JacksonkrJacksonkr 3571 gold badge6 silver badges18 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 5

I think this is what you are looking for:

<?php echo wp_oembed_get('https://www.youtube/watch?v=vZBCTc9zHtI'); ?>

For more details check this documentation.

If you just want to embed a video in the content area, WordPress does this pretty elegantly with oembeds. You just need to paste the URL into the editor and save the post. As long as the embeds are supported, WordPress will work its magic and format your embed.

Your call to do_shortcode() doesn't make sense. As posted, your code is saying, "look through this youtube URL string and run the shortcodes found within it".

do_shortcode() 

Is used to printout shortcode. And you are writting URL in it. No use of this. Try to implement

wp_oembed_get( $url, $args )

This will work for you. So your code will

wp_oembed_get('https://www.youtube/watch?v=vZBCTc9zHtI')

or if you want to specify width then

wp_oembed_get('https://www.youtube/watch?v=vZBCTc9zHtI', array('width'=>400))

It is not working because using a youtube url in the content has nothing to do with shortcodes, which is why it doesn't work when you try to call do_shortcode, what it actually does is to envoke the oEmbed protocol which is a very different thing.

There is an oEmbed shorcode and you probably can do do_shortcode('[oEmbed youtube.....]') but as other answers pointed out, there is an explicit API for that and using the shortcode is the long way to do that.

The most important thing to keep in mind in what you are trying to do, is that oEmbed sends a request to youtube servers and waits to a reply, which means that without some caching this kind of code will slow down the page on every load.

本文标签: Why isn39t this youtube shortcode working