How to add videos to your WordPress sidebar
YouTube has become a bit of a phenomenon on the Internet, and everyone’s rushing to embed videos into their blogs. This is actually a fairly neat thing, especially if you blog a lot about videos or have videos that will emphasize the importance of your posts.
In my last two themes, Structure and Visionary, I created a block that displays the latest video from a category titled “Video” on the front page of the site. Take a look at a live demo that uses the code in this tutorial.
The technique I’ll show you here is completely valid XHTML and works with every browser I could think to test it on. In the example, we’ll use a YouTube video, but there are a number of video sites that you can use this technique on. This tutorial assumes that you are somewhat familiar with XHTML, PHP, CSS, YouTube, and WordPress. You must also be writing a post because we’ll need to use its custom field functionality.
For the sake of this tutorial, we’ll put our video in the sidebar of the theme. You can, of course, put the video wherever you want later.
Find your video
This is obviously the most important step in the process. The example I’ll use is from YouTube. You can check out the Sweatin’ video and use it as an example.
Once you’ve found your video, you need to get the embed URL. On the video’s page, you see a section titled “Embed.” You need to copy that code (see the highlighted portion of this image from our YouTube video):

The embed code that you copied should look like this (yes, it’s a mess):
<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/FE_XpRmtcJw&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/FE_XpRmtcJw&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>
The only part of that code we need is the URL you see in there (notice the missing “&rel” at the end):
http://www.youtube.com/v/FE_XpRmtcJw
Now, hold on to that for later.
Write your post
The next thing you want to do is write a post about your video. That’s the entire reason you’re doing this, right? When you’ve finished your post, you need to create a custom field. This part is fairly simple.
At the bottom of the “Write Post” screen there is a section labeled “Custom Fields.” There are two things you can input, Key and Value. The drop-down list is for custom fields you’ve already used. In the Key input box, type “Video” (make sure you capitalize “Video” because custom fields are case-sensitive).
In the Value input box, type “http://www.youtube.com/v/FE_XpRmtcJw.” Yes, that’s the URL of the video you want to use.

Click the button “Add Custom Field” and you’re done, at least with the posting part. Of course, we still have to code this thing.
The code
As I said earlier, we will put our code in the sidebar, but you can place it in many different places. First, open “sidebar.php” in your favorite text editor. The code we’ll input will probably have to be integrated into whatever type of menu system you have in your sidebar.
Now, you’ll lay down the framework.
<div id="sidebar">
<div class="video">
<div>
<!-- All other code will go here -->
</div>
</div><!-- video -->
</div><!-- sidebar -->
Next, we need to query the posts and begin the loop. In our query, we call for the latest video in the “video” category. You could easily change this to query videos by tag by changing “category_name=video” to “tag=video.”
<?php
// This file displays the latest video article on the front page
rewind_posts();
// Query one post from "video" category
$my_query = new WP_Query('category_name=video&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
We will grab the custom field information and display the post title after that.
// Get the "Video" custom field Key as an array (displays single Value in the video block)
$video = get_post_custom_values($key = 'Video');
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
Now, for the bread and butter of this application — a valid XHTML video embed. We check to see if there’s a custom field Key named “Video” with a Value. If there is, the video is displayed. You can also change the width and height of the video player in this part.
<div class="v<?php echo $i; ?> video">
<?php
// Check to see if custom field "Video" is set and if it has anything in the "Value" field.
if(isset($video[0]) && strcmp($video[0],”)!= 0) {
// Display valid XHTML player for YouTube, Google, MetaCafe, and other video sites
// “echo $video[0];” displays the first item in the array for the custom field “Video”
?>
<object type=”application/x-shockwave-flash” data=”<?php echo $video[0]; ?>” style=”width: 290px; height: 242px; border: none; padding: 0; margin: 0;” id=”video-block-<?php echo $i; ?>”>
<param name=”movie” value=”<?php echo $video[0]; ?>” />
<param name=’wmode’ value=’transparent’ />
<param name=”quality” value=”best” />
<param name=”bgcolor” value=”transparent” />
<param name=”FlashVars” value=”playerMode=embedded” />
</object>
<?php
} // endif
All that’s left to do now is to add a little error checking code and close off the loop. This will make sure you’ve added some type of Value to the custom field Key of “Video.”
// If there is no Value for the custom field Key "Video"
else {
// echo error checking to check custom field errors
echo 'Did not add a video URL to the custom field <strong> Key</strong> of "Video"';
echo '<!-- This user did not add the video URL to the correct custom field -->';
} // endelse
?>
</div> <!-- video -->
<?php endwhile; ?>
Save “sidebar.php.” That’s it for the most part. You might want to consider a few style rules to make it look a little prettier.
Stylesheet
Open “style.css” to add some CSS rules for your extra video feature. Here’s a starting point, but you’ll have to edit it to fit your needs. This code is meant to work with the code above. So, if you’ve changed the width and height of your player, you might have to accomodate.
.video {
display: block;
float: left;
overflow: hidden;
width: 288px;
margin: 0 0 10px 0;
padding: 2px 5px 10px 5px;
text-align: center;
}
.video div {
overflow: hidden;
margin: 0 auto;
padding: 0;
text-align: center;
display: block;
}
object { padding: 0; margin: 0; }
The finished product
I hope by now that you haven’t had too many headaches getting this to work. Here’s an example of how this should look in a completed sidebar.

We’re completely finished now. I hope you’ve enjoyed this tutorial and add a video section to your site today. Be sure to subscribe to the feed to get more WordPress tutorials.
Useful stuff - thanks!
Thanks Darren. Maybe you’ll implement this in one of your theme designs.
brilliant! i’m gonna try it, i was looking to create some sort of sideblog with videos!
i’m here again… is there a way to create a plugin with this?
i know nothing about php and programming…
thank you!!
Justin, have you ever had multiple videos in a player? I’m using your video block and I’d like to have a “next video” link underneath it so users can browse right on the home page. I’ve looked at wordpress’s next_post links and haven’t quite got them to work. Any ideas?
Well, I’ve tried twice to post the whole code but it doesn’t appear to be showing up.
It an interesting plugin. I would however appreciate it if someone could assist me in finding a similar plugin but one that would allow me to post mutiple videos on my African safari vacations blog.
Justin you are a very smart guy.I love the Structure and thanks for that.But why all the hard work when you can copy the URL from the address bar?
Mithila
You can’t just copy the URL from the address bar. It won’t play. The URL in the address bar is to a page. The URL in the embed code is to the video.
Thanks Justin,you saved a lot of trouble.I asked this on your other pages too,but my comment got lost amongst other comments by thankers.Is there any way to remove the “Example” page from the top page list?And how can I get other pages listed there too?You’ll see this if you see my blog,I can only have two pages and the example page which only leads to the same about page?Please help!
Thanks.
Any Idea how to get it working with Vimeo? They do a lot more Resolution, so I choose them to show my work.
I’ve tried everything and I couldn’t put video on my blog…
HELP ME
Mithila Mangedarage
I’m not sure what you’re referring to with “Example Page.”
GPSchnyder
I haven’t tried with Vimeo. I imagine you’ll just have to find the right embed URL.
Thiago Prado
“I’ve tried everything” really doesn’t tell me much.
I do see that you’re using my Structure theme. I have support forums for that. You shouldn’t be following this tutorial as it could be different from the theme.
I’m pretty much a novice at this. I’d like to use the Video Tabs as demonstrated on your demo for the Options dark. After figuring out the Feature Gallery I thought I could figure the Video Tabs out but it ain’t working. I’ve successfully posted the videos to their individual posts, I categorized them under Video and tagged them Video. I don’t understand what or if I need to change anywhere else. Would you direct me to the answers to this quandary. I even successfully used the sidebar widget but I didn’t seem to learn anything that would help me in this instance. The Theme is great and I’m looking forward to getting it together! Thanks.