YouTube and Vimeo Video Gallery With WordPress

In this tutorial we will integrate a YouTube and Vimeo embedded player on to a page on a WordPress web site, without needing to hit the API docs ( which are YouTube API and Vimeo API, if you're interested ). By using the tools available to us in WordPress with a little bit of PHP, we can make a gallery that overcomes the problem of content aggregation.

For example I like the Vimeo player, but it won't always be me uploading the video. A 3rd party may upload to YouTube, and I definitely don't want to keep running back and forth setting sizes on sites to get the embedded player for each video.

In terms of the individual this gives you the power to pick your preferred video site and still be able to adapt if a third party posts a video from another site. For web developers this means you can give the user a control panel and not have to take phone calls for advice on embedded videos.

Step 1 Decide on the Layout First

"Plan out the site first... Retrospective layout is a pain."

The first part of this tutorial would be obvious to most experienced developers and designers. But I will stress it anyway. Plan out the site first, in this case the gallery page. Design the wireframe, decide what width and what height you want the video player to be. Once this is completed you can move on to function. Retrospective layout is a pain.

I used the 960 grid system and made the player 300px wide and 190px high.

Step 2 Get the Embedded Player Code Once!

As you may know, Vimeo and YouTube provide embedded code you can grab to paste into your WordPress site. This gives us the basic player so let's get them.

YouTube Player

Vimeo Player

With both of these embed codes we can customise them, change the width, colours, etc. But this is time consuming and repetitive. So let's make our code do the work.

Step 3 Configure WordPress

Here we can use an excellent plugin called Custom Field Template By Hiroaki Miyashita. Through the settings panel we can input the fields that will become our variables.


And enter the custom post type videos. If you are unsure of how to make custom post types see an easy plugin called, Custom Post Types UI By

Once this is done when you add a new video post you will see the options for the Video ID and Video Site.

The common factor across video sites is the Video ID, at the end of a YouTube URL it looks like this:

The YouTube ID: WhBoR_tgXCI

At the end of the Vimeo URL it looks like this:

The Vimeo ID: 29017795

Because of this we could add more video sites as they adopt this same URL tactic. For now we will stick with the two in question.

So now we have an easy way to assign a video to a post, enter the ID and select the site.

Before we set up the PHP we need to create a page and assign it to a custom template file such as gallery, we can then open our new page and give it basic properties. For more info on custom template files check out the WordPress Codex for child themes and templates.

Step 4 Set the PHP Variables

Now we need to take this information and use it, add a loop to your template file and incorporate your wireframe. For example:

We now have the video posts outputting their title and content. Let's create a couple of variables in the .galvidprevid div (a class, because we have more than one), so we can call the custom meta data with ease.

We can now call the Video ID and Video Site with $videosite and $videoid. So let's get the embedded code from our two sites with the width and height set to the desired size. In the embedded code you can find the ID for the video and replace that with our variable.

So all we have to do is insert our ID by replacing it with our variable $videoid. Let's echo out the content.

Step 5 Create the if Statement

So we have our Video ID in place of the one we had there before. Now we can create an if statement to change the embedded player dependant on the video site selected.

Check it all looks good, add a video and POW! Image below of both the site and admin.

Step 6 Review and Style

So there we have it, I am going to add some of my favourite videos and do a little bit of CSS and it's done.

Here is the CSS I used.

Here we've demonstrated a nice and effective method for implementing embedded video players without the need for huge amounts of code. Did you find this helpful? Let us know what you think in the comments!



Related Articles