Weblog Tools Collection: Removing Width/Height from the Image Uploader

Posted on May 29, 2008 by Ronald Huereca.
Categories: Contributors.
Reader Vivien writes in:
Is there a way to prevent WordPress from inserting the width and the height for images in the new 2.5 media manager?
In short, yes, but it requires you to insert some code into your theme’s functions.php file. Fortunately, there is a WordPress filter we can use called image_downsize, which takes in three arguments (a boolean, an attachment ID, and a size string).
  1. add_filter('image_downsize', 'my_image_downsize',1,3);
All I’m doing in the above filter is setting the filter name, the function to call (my_image_downsize), what priority I want the filter, and how many arguments the function takes. From there, I mimic the function image_downsize in the ‘wp-includes/media.php’ file, but do not return a width or a height. As a result, when the image is sent to the editor, no width or height is present.
  1. function my_image_downsize($value = false,$id = 0, $size = "medium") {
  2.     if ( !wp_attachment_is_image($id) )
  3.         return false;
  4.     $img_url = wp_get_attachment_url($id);
  5.     //Mimic functionality in image_downsize function in wp-includes/media.php
  6.     if ( $intermediate = image_get_intermediate_size($id, $size) ) {
  7.         $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
  8.     }
  9.     elseif ( $size == 'thumbnail' ) {
  10.         // fall back to the old thumbnail
  11.         if ( $thumb_file = wp_get_attachment_thumb_file() && $info = getimagesize($thumb_file) ) {
  12.             $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
  13.         }
  14.     }
  15.     if ( $img_url)
  16.         return array($img_url, 0, 0);
  17.     return false;
  18. }

Download the Code

Here is a sample functions.php file of the code presented in this article. I also used Andrew’s plugin generator to quickly put together a plugin that I will creatively call No Image Width or Height (download link). It will accomplish the same thing for those not comfortable with code. Just unzip, place in your WordPress plugin’s folder, and activate. Thanks Vivien for the interesting question.