I need to add video from youtube in certain place on my page.
I added link in post like https://www.youtube.com/watch?v=GXGR7puGRpg (post contains only this link), and then I added code on my page

<?php
$post_id_63 = get_post( $post_id = 63 );
if($post_id_63) {
    $post_id_63_content = $post_id_63->post_content;
    echo $post_id_63_content;
}
?>

but I get just text link on my page, not video. What did I do wrong? How to fix it and make it work ?

1 Answer
1

This is overview how you can make it working:

First with below code you can detect youtube URL for details Please read:

$reg = preg_match('|^\s*(https?://[^\s"]+)\s*$|im', $post_id_63->post_content, $matches);

 if (!$reg) return false;

 $url =  trim($matches[0]); // youtube video url 

 echo wp_oembed_get($url); // this will return youtube vidoe 

For details please read: Wp_oembed_get

we need to use wp_oembed_get to make URL into video from post content

Thanks!

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *