Eliminate Thumbnails for Custom Post Types

For a recent project, a client required a front end uploader to allow their customers to submit a consent form. Once they completed the form they could upload it as a PDF or JPG. Each form would be linked to a custom post type that contained other information about the user.

The PDFs presented no problems, but because WordPress created thumbnails when processing images, the JPGs were automatically converted into multiple image sizes.

Every consent form uploaded as a JPG added at least three extra files. That meant one hundred uploads would become four hundred, with three hundred files that would never be used. That could add up quickly and use a lot of space on their server for no reason.

Instead of having to go in and delete the extra files, I needed to find a way to nip the process in the bud and eliminate thumbnails for those custom post types before they were created.

When you upload an image, WordPress passes the meta data through a filter which processes the default images sizes: thumbnail, medium and full. You control the sizes under the Settings → Media admin page. You could set all sizes to zero and no extra images would be created whatsoever, though that stopped working in WordPress 4.0. Plus it wouldn’t have worked for this client since they only needed to files eliminated for the custom post type.

No More Extra Files

In order to eliminate thumbnails and stop creating those extra image sizes, I hooked into the intermediate_image_sizes filter.

This is the code I added to functions.php:

add_filter( 'intermediate_image_sizes', bavotasan_custom_intermediate_image_sizes, 999 );
function bavotasan_custom_intermediate_image_sizes( $image_sizes ){
   // @NOTE Be sure to change 'custom_post_type_slug' to the actual slug of your custom post type
   if ( isset( $_REQUEST['post_id'] ) && 'custom_post_type_slug' === get_post_type( $_REQUEST['post_id'] ) )
      return array();

   return $image_sizes;
}

First I checked if they were uploading to the custom post type, then I returned an empty array instead of the default image sizes. Simple enough.

No Thumbnails Whatsoever

If you want to stop this for all uploaded images, you could simplify the code even more:

add_filter( 'intermediate_image_sizes', bavotasan_custom_intermediate_image_sizes, 999 );
function bavotasan_custom_intermediate_image_sizes( $image_sizes ){
   return array();
}

Now you only have to return an empty array. No need to check for a custom post type.

Either one of the functions above will end up saving you a lot of space. If you don’t needs those extra files, eliminate them before they’re created.

If you know of a better solution, feel free to add your thoughts in the comment section below.

Featured image provided by Death to the Stock Photo.