I’m trying to go about this without the use of a plugin, but actions and filters
Is there a way to remove extra image sizes generated by WordPress based on the Custom Post Type? I’ve been trying to work with the intermediate_image_sizes_advanced
filter but it doesn’t look like it has access to $post
or post_type
. So I’m using the function like so:
function filter_image_sizes($sizes) {
global $post;
global $post_type;
if($post->post_type == 'cpt_slides' || $post_type == 'cpt_slides'){
unset( $sizes['thumbnail']);
unset($sizes['medium']);
unset( $sizes['large']);
}
return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'filter_image_sizes');
The Reasoning / Situation
I’ve created a Slides Custom Post Type (like an Image Slider) that uses the Featured Image to allow the user to upload “Slides”. I also have a few custom image sizes, but one specifically for the slides alone. Since the slide images are going to be one static size, I don’t want WordPress to generate all these extra image sizes I’ll never use, hence the question.
May be this filter should work intermediate_image_sizes
Note: This solution will work if you are uploading an image from post edit screen. (tested on localhost with WP-3.8.1)
Part of the problem may be that this filter could end up being called from more than one place, or perhaps a place where no post is associated with the query. In fact the filter being used appears to only be used when the attachment is first created as a part of the metadata.
Doing a bit of testing shows me that when I’m uploading an image the $post and $post_type variables are not set at the location that this filter is used. Likely this happens because the media upload is done as a background process via javascript instead of push a form submit and reloading the page.
WordPress isn’t really setup at all to handle this kind of filtering, it’s perhaps something that should be considered for future updates, I would suggest submitting a ticket to the WordPress Trac and getting some advice there.
Basically WordPress is storing your media elements independent of the post they may be attached to so that you can use the same file easily in more than one place. If WordPress were to allow you to adjust the sizes generated on a more granular basis, you could end up with situations where you uploaded an image for a blog post and then wanted to reuse that image somewhere else as a featured image. The media library is setup to handle this sort of stuff great, but it isn’t setup to regenerate thumbnails on a case by case basis, it’s setup to generate all the required thumbnails because you may use any or all of the thumbnails elsewhere.
The easiest way to handle this would be to use your own media upload function for the custom post type’s metabox when you’re uploading media. This would allow you to bypass the normal WordPress thumbnail process and only create the sizes that you desire when the file is uploaded. Here’s a great tutorial that will help you with that process: http://wp.tutsplus.com/tutorials/attaching-files-to-your-posts-using-wordpress-custom-meta-boxes-part-1/
Maybe you could install this plugin: http://wordpress.org/plugins/simple-image-sizes/
This plugin can add image sizes and override the theme’s custom sizes. Then you can regenerate them.