Youtube Embed code from Video URL – PHP Generator

Youtube video url may not directly work in the embed widget code. Youtube Embed url and video url are different. The user input url in your WordPress or PHP web app can be converted easily to embed url.

Using this tutorial you can easily convert your dynamic Youtube URL to embed url and even embed widget.

In my previous article I have shared how to extract video ID from youtube url.

PHP Code

<?php
$url = 'https://youtu.be/-RdRvv60X2k'; //pass your dynamic url here
$substr = "=";
if (strpos($url, $substr) !== false) {
    $url_arr  = explode("=", $url);
    $video_id = $url_arr[1];
} else {
    $url_arr  = explode("/", $url);
    $video_id = $url_arr[3];
}
$embed_url = "https://www.youtube.com/embed/" . $video_id;
?>

EMBED WIDGET – (html + dynamic url)

<iframe width="400" height="250" 
 src="<?php echo $embed_url;?>"
 frameborder="0" allow="accelerometer; autoplay; encrypted-media; 
 gyroscope; picture-in-picture" allowfullscreen>
</iframe>

Tags: Youtube url to embed url convertion php code, Generating webpage embed widget from youtube url using php, youtube widget generator for wordpress video embed from url not working solutions.

You May Also Like

About the Author: Sreeraj Melath