Building a Simple Vimeo Wordpress Plugin
October 2, 2008The other day I needed to embed a Vimeo clip into my blog. This should have been a straight forward process, given the small amount of code that was needed. Well, the more I explored the various options for getting Vimeo into Wordpress, the more I realized that there really aren’t any great solutions. First, if you copy the code from Vimeo’s site and paste it into your post, the embed will work and video will play. However, if you switch between the rich text editor and the code view, Wordpress changes the <embed> tag to an <img> tag and your video is no more—making it impossible to edit the post later on. Second, the code that comes from Vimeo is not valid XHTML. This is a problem, as I don’t want non-standard code on my site. Finally, the selection of plugins that allow embedding media in your posts are just too bloated. Most allow you to embed every video format under the sun—making the plugins large—and have clunky interfaces and tags.
My solution was to write a simple Wordpress plugin that allows tag-based embedding of Vimeo videos [vimeo 1821974] and outputs valid XHTML to the browser. No other formats are supported (although they could easily be added) and there is no admin menu. This also serves as a great introduction to building small plugins for Wordpress.
Thanks to Martín Aberastegue and his WP-Vidz plugin for the inspiration.
-
Create a new file—locally or on your server—and name it
vimeo.php(or whatever you want, really). Open the empty the file in your favorite text editor. -
Add the required header to the file so Wordpress knows a little more about the plugin. This is done using standard PHP block comments. Don’t forget to add the opening
<?phptag so Wordpress 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 */ -
This plugin has two parts, the main
vimeo_code()function that defines the tag and anadd_filter()call that registers the new tag with Wordpress. -
Create a new function below the header comment block called
vimeo_code()that takes a single argument named$data. This argument will hold the tag that we use in our posts to embed a Vimeo video—in this case:[vimeo 1821974].function vimeo_code($data) { } -
Add two variables within the
vimeo_code()function to store the height and width of the video (this is an area that could use an admin GUI, but remember, we’re going for simplicity here). Set the height and width to values that fit well on your page. Remember to preserve 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; } -
Next, we need some code that takes our Vimeo tag and outputs the proper XHTML to the browser, displaying the correct video based on the ID. To do this we declare a variable named
$data(the same name as our function argument) and use PHP’spreg_replace()function to extract the ID.$patternand$replacementare just placeholders that we’ll change further along in the tutorial.function vimeo_code($data) { $video_width = 450; $video_height = 340; $data = preg_replace($pattern, $replacement, $data); }The first argument for
preg_replace()is a regular expression that matches the format of our tag (this will replace the$patternplaceholder). This will look for a pattern in a chunk of text and if it finds a match, replace it with our specified text.function vimeo_code($data) { $video_width = 450; $video_height = 340; $data = preg_replace("~\[vimeo (.*?)\]~i", $replacement, $data); }The second argument is a string containing the code that will be inserted when our plugin finds the special Vimeo tag in our posts. This is basically an XHTML-valid version of the embed code you get from Vimeo. The last argument—
$datajust tells the function what to perform the first to actions on. In our case, we want to replace the text that is passed to ourvimeo_code()function.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&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&server=vimeo.com\" /> </object>\n", $data ); } -
The last line in our
vimeo_code()function just returns the value of$data. This sends our object code to the browser for display.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&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&server=vimeo.com\" /> </object>\n", $data ); return $data; } -
The very last snippet of code needed to make this plugin work is a call to the
add_filter()Wordpress function. This just tells Wordpress that ourvimeo_code()function should be executed whenever thethe_contenttemplate tag is used. Make sure you also close your script with a?>tag.add_filter('the_content', 'vimeo_code'); ?> -
Save this file and place it in your plugins directory. Once the plugin is activated, you can begin using the special tag
[vimeo 1821974](replacing the ID with the one of your video) in your posts to properly embed Vimeo media on your blog.
Download the completed vimeo.php plugin file.

Martín Aberastegue
Great job Jeff :), and thanks for mention me in your post.
Erwin Heiser
Very nice this, I’m not a Wordpress user but I can see this coming in handy. Great you went to the trouble of producing valid XHTML as well.
Cheers!
Nick Pettit
That’s awesome! Nice work.
My friend Jim and I found that this is something we had to do all the time, so we also built a tool for it, called Validifier, that allows you to convert Flash embed code into valid XHTML.
Hope that helps someone! :D
Jeff Siarto
@Nick
Thanks for the link, that’s a great tool. I just don’t understand why these services aren’t publishing standards-compliant code in the first place. I understand the need for backward compatibility—but they should at least give users the option to generate code that properly validates.
Frank Thomas
Very good! I enjoyed your post immensely. As works to boot! Thanks very much for sharing.
Frank.
nocturnale
Excellent work. I deployed it for use on my blog here at http://www.nocturnale.com/chronicle/2008/10/from-baby-kate-with-love/
Stuart
Hi. I have been desperately trying to this to work. WP always seems to strip out the height and width tags from the object code. I have tried everything i can think of, disabling all other plugins etc. and also editing the
kses.phpfile to allow object and embed tags. Any ideas what I am doing wrong?http://maxeatonmartorell.com/
Jeff Siarto
@Stuart
As far as WP stripping out the height and width tags, are the values properly escaped in
vimeo.php? I would double check and make sure the file you are using matches my code above. Also, I’m running WP 2.6, so there might be a chance that this won’t work on an older version. It has not really been tested on other versions.