Home / functions.php / Display youtube video with embed shortcode
  • How to Display youtube video with embed shortcode


    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.

    ( click code to copy )

    wordpress snippet : PHP

    function youtube($atts) {
    		"value" => 'http://',
    		"width" => '475',
    		"height" => '350',
    		"name"=> 'movie',
    		"allowFullScreen" => 'true',
    	), $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");
    ( click code to copy )

    wordpress snippet : PHP

    [youtube value="http://www.youtube.com/watch?v=1aBSPn2P9bg"]

    ( WordPress codex functions, hooks, in this snippet. )

    add_shortcode, shortcode_atts,
  • Saroj

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

  • Yey

    Isn’t this already built in to WP?

    • 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.

  • 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”.

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

  • 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.


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

    • 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?