X

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.



Code Snippet:


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");


Code Snippet:


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

Comments  Leave a Reply

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

  2. not working

  3. Getting Started with WordPress Shortcodes (+Examples) - Speckyboy Design Magazine June 15, 2012 at 6:53 am

    […] controls [youtube value="http://www.youtube.com/watch?v=1aBSPn2P9bg"%5DSource: wpsnipp.com →Paypal Donation ShortcodeThis is a shortcode helps you to create donation links to your Paypal […]

  4. 30 Extremely Useful Wordpress Code Snippets & Hacks | Web Design Habits October 14, 2011 at 6:23 am
  5. 10 super useful WordPress shortcodes | CatsWhoCode.com September 29, 2011 at 2:16 am

    […] […]

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

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

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

  8. Pixel Solitaire August 1, 2011 at 11:33 pm

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

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

  9. Isn’t this already built in to WP?

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

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

    1. Yes by bad, thanks for pointing that out.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!