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.

.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:

function fixEmbed($oembvideo, $url, $attr) {
  if(strpos($url,'')!== 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);

Posted in Pages, Posts