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

Is merely printing

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

out onto the page.

I know I can display the video with html with

<iframe src="http://www.youtube.com/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?

4 Answers 4

5

I think this is what you are looking for:

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

For more details check this documentation.

5
  • 2
    While this shows oembed retrieval in principle, it's very important to note that it will have WP make a network request every time it's called. For any practical usage the result must be cached. For embeds in posts the results are stored in post meta, if I remember right.
    – Rarst
    Sep 13, 2016 at 18:01
  • @Rarst How can you make sure this is cached?
    – Jacksonkr
    Sep 13, 2016 at 23:00
  • You need to implement caching logic yourself with code if you use this in your own code, using one of WP data storage APIs (transient would be usual choice for such case).
    – Rarst
    Sep 14, 2016 at 9:50
  • @Rarst Do you really think implementation of caching logic will be important if the website going to have only a few videos. Personally I never felt the important of caching for such websites and for website with lots of videos, definitely going to need different approach for adding into the website!! Sep 14, 2016 at 12:49
  • 1
    Count of videos is irrelevant, even one would slow page generation time significantly and unnecessarily. Please consider that you are not writing an answer for a one small site, you are writing an answer for general case of WP development.
    – Rarst
    Sep 14, 2016 at 12:53
2

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".

1
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.com/watch?v=vZBCTc9zHtI')

or if you want to specify width then

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

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.com.....]') 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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