Jeff Siarto - Web Designer

Building a Simple Vimeo Wordpress Plugin

The other day I needed to embed a Vimeo clip into my blog. This should have been a straight for­ward process, given the small amount of code that was needed. Well, the more I explored the var­i­ous options for get­ting Vimeo into Word­press, the more I real­ized that there really aren’t any great solu­tions. First, if you copy the code from Vimeo’s site and paste it into your post, the embed will work and video will play. How­ever, if you switch between the rich text edi­tor and the code view, Word­press changes the <embed> tag to an <img> tag and your video is no more—making it impos­si­ble to edit the post later on. Sec­ond, the code that comes from Vimeo is not valid XHTML. This is a prob­lem, as I don’t want non-​​standard code on my site. Finally, the selec­tion of plu­g­ins that allow embed­ding media in your posts are just too bloated. Most allow you to embed every video for­mat under the sun—making the plu­g­ins large—and have clunky inter­faces and tags.

My solu­tion was to write a sim­ple Word­press plu­gin that allows tag-​​based embed­ding of Vimeo videos [vimeo 1821974] and out­puts valid XHTML to the browser. No other for­mats are sup­ported (although they could eas­ily be added) and there is no admin menu. This also serves as a great intro­duc­tion to build­ing small plu­g­ins for Word­press.

Thanks to Martín Aberastegue and his WP-​​Vidz plu­gin for the inspi­ra­tion.

  1. Cre­ate a new file—locally or on your server—and name it vimeo.php (or what­ever you want, really). Open the empty the file in your favorite text edi­tor.

  2. Add the required header to the file so Word­press knows a lit­tle more about the plu­gin. This is done using stan­dard PHP block com­ments. Don’t for­get to add the open­ing <?php tag so Word­press and your server know what to do with the code.

    <?php
    /*
    Plugin Name: Simple Vimeo
    Plugin URI: http://siarto.com/2008/10/02/vimeo-wp-plugin/
    Description: Valid XHTML Vimeo embed tag
    Author: Jeff Siarto
    Version: 0.1
    Author URI: http://siarto.com
    */
  3. This plu­gin has two parts, the main vimeo_code() func­tion that defines the tag and an add_filter() call that reg­is­ters the new tag with Word­press.

  4. Cre­ate a new func­tion below the header com­ment block called vimeo_code() that takes a sin­gle argu­ment named $data. This argu­ment will hold the tag that we use in our posts to embed a Vimeo video—in this case: [vimeo 1821974].

    function vimeo_code($data) {
    
    }
  5. Add two vari­ables within the vimeo_code() func­tion to store the height and width of the video (this is an area that could use an admin GUI, but remem­ber, we’re going for sim­plic­ity here). Set the height and width to val­ues that fit well on your page. Remem­ber to pre­serve the aspect ratio of your clip so that the video doesn’t look squashed or stretched.

    function vimeo_code($data) {
        $video_width = 450;
        $video_height = 340;
    }
  6. Next, we need some code that takes our Vimeo tag and out­puts the proper XHTML to the browser, dis­play­ing the cor­rect video based on the ID. To do this we declare a vari­able named $data (the same name as our func­tion argu­ment) and use PHP’s preg_replace() func­tion to extract the ID. $pattern and $replacement are just place­hold­ers that we’ll change fur­ther along in the tuto­r­ial.

    function vimeo_code($data) {
        $video_width = 450;
        $video_height = 340;
        $data = preg_replace($pattern, $replacement, $data);
    }

    The first argu­ment for preg_replace() is a reg­u­lar expres­sion that matches the for­mat of our tag (this will replace the $pattern place­holder). This will look for a pat­tern in a chunk of text and if it finds a match, replace it with our spec­i­fied text.

    function vimeo_code($data) {
        $video_width = 450;
        $video_height = 340;
        $data = preg_replace("~\[vimeo (.*?)\]~i", $replacement, $data);
    }

    The sec­ond argu­ment is a string con­tain­ing the code that will be inserted when our plu­gin finds the spe­cial Vimeo tag in our posts. This is basi­cally an XHTML-​​valid ver­sion of the embed code you get from Vimeo. The last argu­ment—$data just tells the func­tion what to per­form the first two actions on. In our case, we want to replace the text that is passed to our vimeo_code() func­tion.

    function vimeo_code($data) {
        $video_width = 450;
        $video_height = 340;
        $data = preg_replace("~\[vimeo (.*?)\]~i", "<object
            width=\"$video_width\"
            height=\"$video_height\"
            data=\"http://vimeo.com/moogaloop.swf?clip_id=$1&amp;server=vimeo.com\"
            type=\"application/x-shockwave-flash\">
                <param name=\"allowfullscreen\" value=\"true\" />
                <param name=\"allowscriptaccess\" value=\"always\" />
                <param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=$1&amp;server=vimeo.com\" />
            </object>\n", $data
        );
    }
  7. The last line in our vimeo_code() func­tion just returns the value of $data. This sends our object code to the browser for dis­play.

    function vimeo_code($data) {
        $video_width = 450;
        $video_height = 340;
        $data = preg_replace("~\[vimeo (.*?)\]~i", "<object
            width=\"$video_width\"
            height=\"$video_height\"
            data=\"http://vimeo.com/moogaloop.swf?clip_id=$1&amp;server=vimeo.com\"
            type=\"application/x-shockwave-flash\">
                <param name=\"allowfullscreen\" value=\"true\" />
                <param name=\"allowscriptaccess\" value=\"always\" />
                <param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=$1&amp;server=vimeo.com\" />
            </object>\n", $data
        );
        return $data;
    }
  8. The very last snip­pet of code needed to make this plu­gin work is a call to the add_filter() Word­press func­tion. This just tells Word­press that our vimeo_code() func­tion should be exe­cuted when­ever the the_content tem­plate tag is used. Make sure you also close your script with a ?> tag.

    add_filter('the_content', 'vimeo_code');
    ?>
  9. Save this file and place it in your plu­g­ins direc­tory. Once the plu­gin is acti­vated, you can begin using the spe­cial tag [vimeo 1821974] (replac­ing the ID with the one of your video) in your posts to prop­erly embed Vimeo media on your blog.

    vimeo tag example

Down­load the com­pleted vimeo.php plu­gin file.

tagged: , ,

18

  • Martín Aberastegue

    Great job Jeff :), and thanks for men­tion me in your post.

  • Erwin Heiser

    Very nice this, I’m not a Word­press user but I can see this com­ing in handy. Great you went to the trou­ble of pro­duc­ing valid XHTML as well.
    Cheers!

  • Nick Pettit

    That’s awe­some! Nice work.

    My friend Jim and I found that this is some­thing we had to do all the time, so we also built a tool for it, called Valid­i­fier, that allows you to con­vert Flash embed code into valid XHTML.

    Hope that helps some­one! :D

  • Jeff Siarto

    @Nick
    Thanks for the link, that’s a great tool. I just don’t under­stand why these ser­vices aren’t pub­lish­ing standards-​​compliant code in the first place. I under­stand the need for back­ward compatibility—but they should at least give users the option to gen­er­ate code that prop­erly val­i­dates.

  • Frank Thomas

    Very good! I enjoyed your post immensely. As works to boot! Thanks very much for shar­ing.

    Frank.

  • nocturnale

    Excel­lent work. I deployed it for use on my blog here at http://​www​.noc​tur​nale​.com/​c​h​r​o​n​i​c​l​e​/​2​0​0​8​/​1​0​/​f​r​o​m​-​b​a​b​y​-​k​a​t​e​-​w​i​t​h​-​love/

  • Stuart

    Hi. I have been des­per­ately try­ing to this to work. WP always seems to strip out the height and width tags from the object code. I have tried every­thing i can think of, dis­abling all other plu­g­ins etc. and also edit­ing the kses.php file to allow object and embed tags. Any ideas what I am doing wrong?

    http://​max​eaton​mar​torell​.com/

  • Jeff Siarto

    @Stuart
    As far as WP strip­ping out the height and width tags, are the val­ues prop­erly escaped in vimeo.php? I would dou­ble check and make sure the file you are using matches my code above. Also, I’m run­ning WP 2.6, so there might be a chance that this won’t work on an older ver­sion. It has not really been tested on other ver­sions.

  • shafalcon

    jeff…thank you so much for putting this thing together. it’s the only thing that’s worked on my site. you rock!!!
    shafal­con

  • Terry Brooks

    Thanks, you’re a star

    Terry

  • Mark

    Hi there,

    This is just what I was look­ing for and works a treat. The only prob­lem is that some of my videos are dif­fer­ent size and there­fore is there a way to add some extra code to enable the user to input the width and height in the short­code. For exam­ple you could use:

    [vimeo 123456 500 300]

    to dis­play a vimeo video with the ID 123456 and have it dis­play 500 wide by 300 px high?

    Your help would be much appre­ci­ated. Thanks

  • Jeff Siarto

    @Mark

    That fea­ture was some­thing that I orig­i­nally con­sid­ered but I really wanted to make the sim­plest plu­gin pos­si­ble. This site fea­tures two dif­fer­ent col­umn widths (home page and arti­cle page) and I ran into the same prob­lem. I am work­ing on a fol­low up to this arti­cle that should include vari­able widths and I may release the new ver­sion as an offi­cial plu­gin. Check back soon.

  • Sarah

    Thank you so much for this tut!

    I am also inter­ested in what Mark requested, basi­cally being able to define the video’s width and height inside the short­code. Any tips on how this can be done?

  • marlus araujo

    Wow! Great plu­gin exam­ple.
    Is there a way to fil­ter cus­tom tags?

    I’ve tryed

    add_filter(‘get_post_meta’, ‘vimeo_​code’);

    but with no suc­cess

  • Devon

    Well, first let me start by say­ing… I’m not to quick with code. In fact, I have been steadily learn­ing because of Word­press.
    I am con­fused as to if this plug in will solve my prob­lem.
    See, I want the vimeo video to have an image that auto­mat­i­cally resizes for my posts. But every­one says it’s easy with youtube ( I have no idea how they find the image!)
    But I love vimeo, and I just want to add videos with an auto­matic image resized for my posts. Please enlighten me to this plug in and how I would go about doing, so…
    I feel so hor­ri­ble that I have to use up your time for this ques­tion.

    –Devon

  • Jeff Siarto

    @Devon

    My plu­gin doesn’t do auto­matic video resiz­ing. I had a fixed width I needed to deal with and wrote this to solve that spe­cific problem–along with insert­ing standards-​​compliant code. Check the Word­press Plu­gin Direc­tory, I know there has to be sim­i­lar Vimeo plu­g­ins.

  • Will

    Excel­lent tuto­r­ial. Is it pos­si­ble to spec­ify the other embed options? Specif­i­cally color, title/​byline tog­gling?

  • Jeff Siarto

    @Will

    It is pos­si­ble to add those options–but you have to mod­ify the plu­gin. When I orig­i­nally wrote this, it was for a very spe­cific need of mine. There are newer, more flex­i­ble Vimeo short­code plu­g­ins out there now:

    http://​word​press​.org/​e​x​t​e​n​d​/​p​l​u​g​i​n​s​/​l​u​x​-​v​i​m​e​o​-​s​h​o​r​t​code/

    If you’re feel­ing adven­tur­ous, try and mod­ify my plu­gin to accept new values–you should be able to see how every­thing works just by look­ing at the plu­gin code.

Recommended

Also of Interest