wordpress snippets last updated   : July 14, 2014
we now have 634 snippets

Display youtube video with embed shortcode

wordpress snippet

Adding this snippet to your wordpress theme will let you embed youtube video with a simple shortcode to your posts. Just add the youtube url to the video and your ready to go although you can specify a few paramaters like width, height, name, etc.

snippet :  PHPcopy
function youtube($atts) {
	extract(shortcode_atts(array(
		"value" => 'http://',
		"width" => '475',
		"height" => '350',
		"name"=> 'movie',
		"allowFullScreen" => 'true',
		"allowScriptAccess"=>'always',
	), $atts));
	return '<object style="height: '.$height.'px; width: '.$width.'px"><param name="'.$name.'" value="'.$value.'"><param name="allowFullScreen" value="'.$allowFullScreen.'"></param><param name="allowScriptAccess" value="'.$allowScriptAccess.'"></param><embed src="'.$value.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"></embed></object>';
}
add_shortcode("youtube", "youtube");
snippet :  PHPcopy
[youtube value="http://www.youtube.com/watch?v=1aBSPn2P9bg"]

  • Saroj

    did you mean
    [youtube value="http://www.youtube.com/v/UthUv3Njy08"]

    • http://wpsnipp.com Kevin Chard

      Yes by bad, thanks for pointing that out.

  • Yey

    Isn’t this already built in to WP?

    • http://wpsnipp.com Kevin Chard

      Yes and no, yes you can insert youtube videos however using shortcode will give you a few benefits that inserting with the regular method will not. Including but not limited to if you need to change your design down the road you can change the default values and all your videos will change, where as the regular method you would need to edit every post.

  • http://www.facebook.com/profile.php?id=100002589565005 Pixel Solitaire

    Cool piece of code but it seems that your function include the parameter to display the control buttons without using it anywhere in the return output. It’s OK if you need the default like shown but useless trying to put it to “0″.

    • http://wpsnipp.com Kevin Chard

      Pixel, thanks for pointing that out, although it would still function properly it should be according to the specification. I will update the snippet,

  • http://wpsnipp.com Kevin Chard

    The full screen option is still within the snippet. Both as a parameter as well as an attribute I only made a few changes to the HTML. Ill have to check out your code and thanks for the link back. You should contribute to wpsnipp.com as an author.

    http://wpsnipp.com/index.php/contribute/

  • http://loneplacebo.com/ Tony Hue

    I noticed placing just the Youtube links in a blog post now automatically embeds the video as well. 

    • http://wpsnipp.com Kevin Chard

      Hi Tony how are you.

           You are right Tony, however some people want to have a little more control when embed into the page including size etc, but you are correct for most just adding the youtube url will work just fine.

  • Pingback: 10 super useful WordPress shortcodes | CatsWhoCode.com

  • Pingback: 30 Extremely Useful Wordpress Code Snippets & Hacks | Web Design Habits

  • Pingback: Getting Started with WordPress Shortcodes (+Examples) - Speckyboy Design Magazine

  • mkalik

    not working

  • tertr

    is it work in page sidebar,shortcode embedded using text widget?

jqsnipp.com | we just launched jquery snippets website