• Posted: 2010-11-09
  • Author: Alex Chousmith
  • Tags:

Adjust Vimeo Auto-Embed Size

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
<?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);
?>

Instructions:

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.


Related links:
http://codex.wordpress.org/Embeds
Share this snippet

If you like this snippet, share it with friends!