WordPress How To Get Embed HTML of Video Using oEmbed By Video Link

Hello Friends, WordPress have feature to convert to its embed HTML by video link/URL, meaning if we put link of video in editor it automatically converts that video link to its embed HTML code. But as we know we always like to use simple things in different way so that somtimes we want to use this feature in our theme So user enter video link in any custom field and we want to convert that video link to its embed HTML, So it shows videos automatically as it is showing link in editor.

We can do this by using the_content filter but for example if we are showing that video after title and before content using the_content filter then if we using any plugin which use the_content filter to add social sharing buttons or related posts then it will show twice because the_content called 2 times, first call to parse video link and second to show content.So to overcome this problem I found one wordpress functions who return Embed HTML of Video using oEmbed by passing link of video.

wp_oembed_get takes a URL and Attempts to fetch the embed HTML for it using oEmbed. Here is list of sites  which you can embed from. So this method will work only for sites exists in lists, So if you are using any other website video link then it will not work. But as we can see it support mostly all famous video sites, So this is not a big issue.

We can use wp_oembed_get like this :

<?php $embed_code = wp_oembed_get('http://www.youtube.com/watch?v=8h85FZL5Khk'); ?>

Get embed html code by link by passing width argument :

<?php $embed_code = wp_oembed_get('http://www.youtube.com/watch?v=8h85FZL5Khk', array('width'=&gt;400)); ?>

So wp_oembed_get is function by which you can get embed HTML using oEmbed by passing link of video.

Subscribe to PHP Freelancer

Enter your email address: