youtube embed code with PHP

The get_youtube_embed function below generates embed code for a youtube video given its id. You also have the option to set whether the video should be played automatically or not once the web page is loaded.

/*
*	get_youtube_embed returns embed code for a youtube video given its id.
*/
function get_youtube_embed($youtube_video_id, $autoplay=false)
{
	$embed_code = "";
 
	if($autoplay)
		$embed_code = '<embed src="http://www.youtube.com/v/'.$youtube_video_id.'&rel=1&autoplay=1" pluginspage="http://adobe.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" width="480" height="395" bgcolor="#ffffff" loop="false"></embed>';
	else
		$embed_code = '<embed src="http://www.youtube.com/v/'.$youtube_video_id.'&rel=1" pluginspage="http://adobe.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" width="450" height="376" bgcolor="#ffffff" loop="false"></embed>';
	return $embed_code;
}

Sample Code:

Get the youtube embed code for this video http://www.youtube.com/watch?v=5P6UU6m3cqk

echo get_youtube_embed("5P6UU6m3cqk");

Get Embed code for this video http://www.youtube.com/watch?v=wZy3ZJOxh9E with option autoplay set to true.
echo get_youtube_embed("5P6UU6m3cqk", true);

This entry was posted in PHP and tagged . Bookmark the permalink.

One Response to youtube embed code with PHP

  1. Stanley says:

    Hey, do you know the correct code so that once the video is complete, it embeds a next code defined youtube video?

    Cheers

Leave a Reply

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

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>