Fix Vimeo embed width in WordPress

You may have had issues with auto embedding Vimeo videos ( via plain url ) into your WordPress posts/pages and having them ignore your maximum width setting. Thanks to Alex Chousmith, we can fix this issue. Simply paste the following filter into the functions.php file in your theme:

// Fix Vimeo Auto Embed
function fixEmbed($oembvideo, $url, $attr) {
if(strpos($url,'vimeo.com')!== false) {
// check if url is for Vimeo video
$width = 0;
$height = 0;
$newheight = 0;
$attrstart = strpos($oembvideo,'width="');
if($attrstart !== false) {
$attrstart += 7;
$width = substr($oembvideo, $attrstart, strpos($oembvideo,'"',$attrstart+1)-$attrstart);
$attrstart = strpos($oembvideo,'height="');
if(($attrstart !== false) && $width>0) {
$attrstart += 8;
$height = substr($oembvideo, $attrstart, strpos($oembvideo,'"',$attrstart+1)-$attrstart);
$newheight = round($height*$attr['width']/$width);
$oembvideo = str_replace('height="'.$height,'height="'.$newheight, str_replace('width="'.$width,'width="'.$attr['width'], $oembvideo));
}
}
}
return $oembvideo;
}
add_filter('embed_oembed_html', 'fixEmbed', 10, 3);

Note: You’ll need to go back and re-save any posts pages for the change to take effect.

Published by

David

I'm a mixed bag of assorted goodies. More about me here.

Leave a Reply