# Quick Tip: Your Own Video Shortcode

You probably use a video embedding plugin or you just use the embed codes of video sites. But there's a third, easier way to embed videos in your WordPress site: a simple (but useful) video shortcode.

## Why Should I Use a Video Shortcode?

• Because video embedding plugins are just another little burden for your blog. They take some space on your disk (granted, not more than 1MB), they always query your database for their options and you need to learn how to use the plugins.
• Because embedding the codes of the video sites can be corrupted – especially when you switch between the WYSIWYG editor and the HTML editor.
• And most importantly: Because shortcodes are awesome! They're easy to use, they can have the functionality of many plugins and their code doesn't break in your posts!

## Exploring the Video Sites

We're going to work with 7 video hosting sites:

2. Vimeo
3. Dailymotion
4. Yahoo! Screen
5. Blip.tv
6. Veoh
7. Viddler

Let's see what their embed codes look like:

The default embed code looks like this:

 1 2 /* Original video: youtube.com/watch?v=dQw4w9WgXcQ */  3  

But there's one option, the "privacy-enhanced mode" which adds "-nocookie" to the domain and we're going to use it in our shortcode.

### Vimeo

 1 2 /* Original video: vimeo.com/36804448 */  3  

Simple and elegant. That's why people love Vimeo.

### Dailymotion

 1 2 /* Original video: dailymotion.com/video/xhwpbg_bridgestone-15-sec-spot_auto */  3 
BridgeStone 15 Sec spot by DailymotionUSA 

I think adding a link below the embed code is just not cool, so we're not adding that to our shortcode.

### Yahoo! Screen

 1 2 /* Original video: screen.yahoo.com/mysterious-death-of-500-fish-in-german-lake-blamed-on-urinating-swimmers-29322943.html */  3 


The embed code is a little weird on Yahoo! Screen but I found a way to shorten it which will be easier to use in our shortcode.

### Blip.tv

 1 2 /* Original video: blip.tv/mister-glasses/episode-7-5600357 */  3  

This is a hard one – this doesn't have the video ID (from the video's URL) in the embed code. But thanks to some research, I figured out how to use the ID! :)

### Veoh

 1 2 /* Original video: veoh.com/watch/v27458670er62wkCt */  3 
Watch Intense Cat in Animals  |  View More Free Videos Online at Veoh.com 

Ah, the <object> tag... Don't worry, we're not going to use it!

### Viddler

 1 2 /* Original video: viddler.com/v/978c9ba2 */  3  

That's all. Now, let's get to the fun part!

## The Shortcode: [vid]

We'll create 4 attributes for this shortcode – the name of the site, the ID of the video and the width and the height of the video. You can set some default values for the attributes:

 1 2 function vid_sc($atts,$content=null) {  3  extract(  4  shortcode_atts(array(  5  'site' => 'youtube',  6  'id' => '',  7  'w' => '400',  8  'h' => '250'  9  ), $atts)  10  );  11 }  12 add_shortcode('vid','vid_sc');  Then comes the part where the function generates the $src variable which generates the src attribute for the iframe:

 1 2 // YouTube with "privacy-enhanced mode":  3 if ( $site == "youtube" ) {  4 $src = 'http://www.youtube-nocookie.com/embed/'.$id;  5 }  6 // Vimeo:  7 else if ($site == "vimeo" ) {  8  $src = 'http://player.vimeo.com/video/'.$id;  9 }  10 // Dailymotion:  11 else if ( $site == "dailymotion" ) {  12 $src = 'http://www.dailymotion.com/embed/video/'.$id;  13 }  14 // Yahoo! Screen with some cuts in the URI:  15 else if ($site == "yahoo" ) {  16  $src = 'http://d.yimg.com/nl/vyc/site/player.html#vid='.$id;  17 }  18 // Blip.tv with some "hacks" in the URI:  19 else if ( $site == "bliptv" ) {  20 $src = 'http://a.blip.tv/scripts/shoggplayer.html#file=http://blip.tv/rss/flash/'.$id;  21 }  22 // The Veoh URI has some hacks, too:  23 else if ($site == "veoh" ) {  24  $src = 'http://www.veoh.com/static/swf/veoh/SPL.swf?videoAutoPlay=0&permalinkId='.$id;  25 }  26 // Viddler:  27 else if ( $site == "viddler" ) {  28 $src = 'http://www.viddler.com/simple/'.$id;  29 }  And of course, we return the output. Here's the full code of our brand new video shortcode:  1 2 function vid_sc($atts, $content=null) {  3  extract(  4  shortcode_atts(array(  5  'site' => 'youtube',  6  'id' => '',  7  'w' => '600',  8  'h' => '370'  9  ),$atts)  10  );  11  if ( $site == "youtube" ) {$src = 'http://www.youtube-nocookie.com/embed/'.$id; }  12  else if ($site == "vimeo" ) { $src = 'http://player.vimeo.com/video/'.$id; }  13  else if ( $site == "dailymotion" ) {$src = 'http://www.dailymotion.com/embed/video/'.$id; }  14  else if ($site == "yahoo" ) { $src = 'http://d.yimg.com/nl/vyc/site/player.html#vid='.$id; }  15  else if ( $site == "bliptv" ) {$src = 'http://a.blip.tv/scripts/shoggplayer.html#file=http://blip.tv/rss/flash/'.$id; }  16  else if ($site == "veoh" ) { $src = 'http://www.veoh.com/static/swf/veoh/SPL.swf?videoAutoPlay=0&permalinkId='.$id; }  17  else if ( $site == "viddler" ) {$src = 'http://www.viddler.com/simple/'.$id; }  18  if ($id != '' ) {  19  return '';  20  }  21 }  22 add_shortcode('vid','vid_sc'); 

Tip within the quick tip: Take note that the iframe has two CSS classes: vid and iframe-\$site (e.g. iframe-youtube). You should add vid {border:0;} to your CSS file since we didn't define the frameborder attribute in our iframe tag.

## Usage Examples

The default usage is simple enough:

[vid site="youtube" id="dQw4w9WgXcQ" w="600" h="340"]

But to make it even simpler, we set default values for site, w and h. So, if you want to embed a YouTube video, you can just use it like this:

[vid id="dQw4w9WgXcQ"]

You should change the width and the height to match your blog. Also, if you use Vimeo more than YouTube, you can change the default site value to "vimeo".

That's it! Add this to your functions.php file and you can start using the shortcode. Enjoy!

Update: We've added a usage example section to the article now to make things clearer.