YouTube and Vimeo Video Gallery With WordPress

YouTube and Vimeo Video Gallery With WordPress

Tutorial Details
  • Program: WordPress
  • Version: 3+
  • Difficulty: Medium
  • Estimated Completion Time: 1 hour

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

<iframe width="560" height="315" src="http://www.youtube.com/embed/WhBoR_tgXCI" frameborder="0" allowfullscreen></iframe>

Vimeo Player

<iframe src="http://player.vimeo.com/video/29017795" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><p><a href="http://vimeo.com/29017795">Experience Zero Gravity</a> from <a href="http://vimeo.com/bettywantsin">Betty Wants In</a> on <a href="http://vimeo.com">Vimeo</a>.</p>

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.

Enter

[Video ID]
type = textfield

[Video Site]
type = select
value = youtube # vimeo

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 WebDevStudios.com

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:

http://www.youtube.com/watch?v=WhBoR_tgXCI

The YouTube ID: WhBoR_tgXCI

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

http://vimeo.com/29017795

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.

<?php
/* Template Name: gallery
*/
?>
<?php get_header() ?>

<?php get_footer() ?>

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:

<div id="galvidcontainer">
<h1>Videos</h1>
     <?php
          $args = array( 'post_type' => 'videos', 'posts_per_page' => 10 );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <div class="galvidpre">
               <div class="galvidprevid">
               </div>
               <div class="galvidpretext">
                    <h1><?php the_title() ?></h1>
                    <p>
                    <?php $words = explode(" ",strip_tags(get_the_content()));
                    $content = implode(" ",array_splice($words,0,10));
                    echo $content; ?>
                    </p>
               </div>
          </div>
     <?php endwhile; ?>
</div>

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.

<?php
$videosite = get_post_meta($post->ID, 'Video Site', single);
$videoid = get_post_meta($post->ID, "Video ID", single);
?>

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.

<iframe src="http://player.vimeo.com/video/29017795" width="300" height="190" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>

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

echo '<iframe src="http://player.vimeo.com/video/'.$videoid.'" width="300" height="190" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';

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.

<?php
if ($videosite == 'vimeo')
{
	echo '<iframe src="http://player.vimeo.com/video/'.$videoid.'" width="300" height="190" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
}
else if ($videosite == 'youtube')
{
	echo '<iframe width="300" height="190" src="http://www.youtube.com/embed/'.$videoid.'" frameborder="0" allowfullscreen></iframe>';
}
else
{
	echo 'Please select a Video Site via the WordPress Admin';
}
?>

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.

h1 {font-size:20px;}

#galvidcontainer {
  width:940px;
  margin:0 auto;
}

.galvidpre {
  width:300px;
  height:325px;
  float:left;
  margin:5px;
  background-color:#ccc;
}

.galvidprevid {
  width:300px;
}

.galvidpretext {
  width:280px;
  padding:10px;
}

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!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.graphismeo.com Graphismeo

    Thank you for this article :)

  • http://ahrengot.com/ Jens Ahrengot Boddum

    Cool idea – This is an obvious place to use FitVid.js to easily fix bugs like this: http://cl.ly/1l0W2w1V2P352H352U2D when you scale the browser.

    I’ve also found that clients don’t usually get the who video id thing, so it might be better to enable them to paste full URL’s and then use RegEx to extract the ID.

    • http://crossoversounds.com Max McKenzie
      Author

      thanks for the comment Jens, I wasn’t too concerned with resizing etc with the tutorial, hence why I didn’t to change the theme. Cheers for the link to fitvid.js

      indeed using regular expressions would be better, but not everyone has the capability to code it.

  • tiak

    Hello,
    Thanks for this article,it’s working well with youtube but it’s not working with vimeo.

    Front

    Back

    have you got an idea ?

    • http://crossoversounds.com Max McKenzie
      Author

      hay Tiak

      I need a few more clues, could you link me to the page and send via http://pastebin.com/ the code. cheers

      • http://www.martineetbernard.com bernard

        (sorry for my poor english)
        @ tiack
        is it due to the gallery.php downloaded file ? There is an error: its written “vemio”, no ?

        • http://www.crossoversounds.com Max McKenzie
          Author

          Yes thats a spelling mistake, its spelt vimeo

          i apoligies for the late reply, your bad english coupled with my dyslexia :) hopefully there are no more spelling mistakes.

  • Pingback: YouTube and Vimeo Video Gallery With WordPress | Shadowtek | Hosting and Design Solutions

  • http://certainstrings.com Justin Tucker

    Nice and straight forward. I’d like to see the tutorial expand on how to create your own custom post type from scratch.

  • Kyle

    Thanks for the tutorial, and not to be a downer, but I think it would have been much better to write this without having to rely on another plugin.

    I guess the point could be made that this makes it easier for not technical people.

    Maybe the tutorials here would be better slit up into different categories, wiht some targeted at the novice/non programmer, and others for advanced developers.

    Regardless of my opinion, anything contributed is appreciated, so thank you.

    • Cory

      @kyle do you mean using something like custom metaboxes instead of the Custom Field Template plugin?

    • http://crossoversounds.com Max McKenzie
      Author

      hay kyle, (and to a certain extent justin)

      Yes it’s geared towards the more non technical people. You can see http://codex.wordpress.org/Post_Types for details on custom post types.

      I see no reason to split it up into novice and advanced, if your advanced then you already know about post types etc so there would be no need to inform.

      plugins are one of the major USP’s of WordPress, and is what makes it so accessible.

  • Pingback: Just another WordPress.com site | Open Knowledge

  • http://www.tristarwebdesign.co.uk/ Paul Weston

    Thought this was a great article and one that will be very useful to me as I move forward with learning WordPress. I am still new to WordPrrss and have just started to use it for my news feeds. I will be using it more and building full sites in the future so articles like this are excellent for me so I can learn what WordPress can do. I like this tutorial because I am finding clients want video integrated in their sites more and more. I found the tutorial easy to follow with the resources and guides that went with it. Great job and I look forward to more from you.

  • http://www.eddiechungdesign.com eddie

    Thanks for the tutorial, great stuff.

    I was just wondering how would I incorporate this to a popup lightbox effect?

    • http://www.crossoversounds.com Max McKenzie
      Author

      ohhh thats a good question sorry it took so long to reply,

      I would set a featured image for the post and use that for the display of all the videos,

      so it whould be something like

      <a href="” rel=”lightbox[nameofgallery]” title=”">

      the href permalink would then go to a single-videos.php template (provided that your post type) page with just the php script in it. That should work.

  • http://tutspress.com/ Tutspress

    Great tutorial thanks. Can i ask you, how can i create a post type without plugin?

    • http://amayamedia.com Rene Merino

      If you install the Custom Post Plugin in manage custom post it gives you the code.

    • http://www.crossoversounds.com Max McKenzie
      Author

      Custom Post Types UI By WebDevStudios.com

      stated in step 3

    • http://crossoversounds.com Max McKenzie
      Author

      Ah just saw what you actually wrote,

      this is the easyist way,

      add_action( ‘init’, ‘create_post_type’ );
      function create_post_type() {
      register_post_type( ‘article’,
      array(
      ‘labels’ => array(
      ‘name’ => __( ‘Articles’ ),
      ‘singular_name’ => __( ‘Article’ )
      ),
      ‘public’ => true,
      ‘has_archive’ => true,
      ‘thumbnail’ => true,
      ‘rewrite’ => array(‘slug’ => ‘articles’)
      )
      );
      }

  • Pingback: Видеогалерея YouTube и Vimeo на WordPress | Wordpresso

  • http://rudyland.net Rudy

    Thanks for the great tutorial – I was able to modify the code to integrate with Jigoshop for a client selling stock photography and video footage. I needed a way to display YouTube videos in the shopping cart, and your solution works perfectly.

    • http://www.crossoversounds.com Max McKenzie
      Author

      Cool i would love to see the link to that

  • Pingback: Embedding videos from facebook | Daniel Sliwka design

  • http://bucurion.info/ Bucur

    excelent articole,best i love

  • Pingback: Los mejores 35 tutoriales para WordPress marzo del 2012 | El blog de David Chávez s

  • Gabe Rossi

    This is great I’m using it on my site, is there a way to add a neat pagination for further browsing?

    • http://crossoversounds.com Max McKenzie
      Author

      Pagination still confuses me, but provided it’s in the Custom post type page, the default pagination should work.

  • Guilherme

    (Sorry for my poor english)

    Nice tutorial, but didn’t work here.

    When I click on ‘Add Video’, at the Custom Fields they don’t appear like on the image above (with the “Video ID” label, etc.)

    I did exactly what you tell on the tut, but couldn’t reach the point. I think the error is on the ‘Custom Posts UI’ part, because I don’t know how to do that correctly.

    Hope you can help me.

    • http://crossoversounds.com Max McKenzie
      Author

      yes someone else pointed that out i spelt Vimeo wrong in the custom post types UI,

      i wrote Vemio, it is infact vimeo.

  • Shawn

    I can’t figure out what I am supposed to be doing with the PHP file you have provided. I feel like a few steps were left out for us, less acquainted users.

    This in particular confuses me:

    Before we set up the PHP we need to create a page and assign it to a custom template file such as gallery,

    After I create a page, how do I assign it to a custom template file other than the ones that are provided. This leads me to my original question, about what folder I need to put the php file in.

    Thanks!

  • http://www.linktwitch.com adzay

    Thanks I was able to make this successfully. Do you know if there is an automated way for me to make the videos (that i create through this method) automatically generate a post in the default word-press format .? For example, if you click my site. I want it so that the videos in the ‘Video gallery’ page are able to show up in my slider,

  • http://www.linktwitch.com Adzay

    Actually ignore my email above this one is more important. How do I add a page break?.. It only shows 10 but I want there to be a “Next page” link below.

    Thanks

  • http://www.whattaf.com/ Pressa

    Nice tutorial, but it Kind of feels easier with a plugin, wordpress plugin for this matter.

  • jury

    The package could not be installed. No valid plugins were found.

    Plugin install failed.

  • Akeisa Lowe

    Nice Tutorial :) I’ve been searching the net for months to find something like this! THANK YOU!

    I have a Video page that displays all videos, and I turned each video item into a link that directs the viewer to a single-videos page. Now on single-video pages the selected video is a larger size at the very top. Underneath are the rest of the videos from the main Videos page.

    My question is, how can I remove that selected video from list on the single-videos page?

    Pastebins//

    -single-videos.php http://pastebin.com/jt6dBdYP

    -video-gallery.php http://pastebin.com/qD6JF1BS

    Here is a quick example of the current layout.

    http://i35.tinypic.com/30sdhyb.png

    The “Selected Video” with green border is what I’d like to remove, only from that video’s single page…

    Possible?

    I hope so :x
    I’d appreciate the help :)

    Thanks.

  • Akeisa Lowe

    How can I add a “Featured Video” section so the client can have whatever video desired on the home page…?

    This Events tutorial has an example of what I am referring to//
    http://www.noeltock.com/web-design/wordpress/how-to-custom-post-types-for-events-pt-2/

    I am currently using a text widget and adding the video with the YouTube iframe. But I find it quite messy. I want to implement the video inside of the page template instead of using the widget…

    Please Help…
    I’d appreciate it :)

  • Akeisa Lowe

    How can I get the number of views for each video?