Changing the Dimensions for Resizing Images

A while back, I added the ability to resize images into EWWW I.O. and there was much rejoicing. Everything was beautiful, until I got a request to make it better. At first, my thought was only negative; I don’t like adding new options, and I fight the constant “feature creep” that comes with a plugin that’s been around a while. Generally, if you can give me a good use case though, my resistance crumbles, and whenever I can find a few spare moments, I make the magic happen. I really didn’t want to add another pair of dimensions though. I’ve been making some good progress on simplifying the EWWW I.O. options, and this felt like a step back.

Then it hit me, and I remembered one of the most powerful functions in WordPress: apply_filters(). As I’ve said before, filters are a big part of what makes WordPress so powerful (and extensible). With that in mind, I realized that I could make the resizing function within EWWW I.O. infinitely configurable by adding a single well-placed filter:

list( $maxwidth, $maxheight ) = apply_filters( 'ewww_image_optimizer_resize_dimensions', array( $maxwidth, $maxheight ), $file );

And with that one line, the floodgates are open. Any function registered on ewww_image_optimizer_resize_dimensions filter will receive an array with the maximum width and maximum height that will shortly be applied to an uploaded image. Thus, the same function must return an array with the desired maximum width and maximum height. In practice, that would look something like this:

Plugin Name: EWWW Custom Resize
Version: .1

add_filter( 'ewww_image_optimizer_resize_dimensions', 'ewww_custom_resize', 10, 2 );
function ewww_custom_resize( $dimensions, $filename ) {
   if ( empty( $_REQUEST['post_id'] ) && ! empty( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], 'media-new.php' ) ) {
      return array( 0, 0 );
   return $dimensions;

In this particular case, we are checking for images uploaded using the page at Media->Add New. When you upload an image on that page, the post_id is 0 (which is considered “empty”), and the referring page is “media-new.php”. The function above returns zeroes, which disables the resizing. This allows you to set dimensions for Resize Media Images so that images uploaded to posts/pages are resized, but images uploaded on the Add New page are not resized. If you want to do something different, just substitute in your own values or change the conditions to match whatever you like.

As with all code samples, you can save this into a file like ewww-custom-resize.php, put it in your plugins folder, and then activate the EWWW Custom Resize plugin. Happy Optimizing!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.