X

Adjust Vimeo Auto-Embed Size

If you have Auto-embeds enabled on your WordPress site, and are trying to embed a Vimeo video by simply pasting in the URL, you may notice that the Embed code comes out as a different size than what is specified in the "Maximum embed size" of your Media Settings.

If you have Auto-embeds enabled on your WordPress site, and are trying to embed a Vimeo video by simply pasting in the URL, you may notice that the Embed code comes out as a different size than what is specified in the “Maximum embed size” of your Media Settings.

.Adding a quick filter like this, to your theme’s functions.php file, will adjust the width/height of the auto-embedded iframe from Vimeo, to match the WIDTH of your Maximimim Embed Size.

Code Snippet:

<?php
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);
?>

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!