2

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.

3

3 Answers 3

8
+50

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)

add_filter( 'intermediate_image_sizes', 'ravs_slider_image_sizes', 999 );
function ravs_slider_image_sizes( $image_sizes ){

    // size for slider
    $slider_image_sizes = array( 'your_image_size_1', 'your_image_size_2' ); 

    // for ex: $slider_image_sizes = array( 'thumbnail', 'medium' );

    // instead of unset sizes, return your custom size for slider image
    if( isset($_REQUEST['post_id']) && 'your_custom_post_type' === get_post_type( $_REQUEST['post_id'] ) )
        return $slider_image_sizes;

    return $image_sizes;
}
4
  • That looks like it worked! I believe add_fileters should be changed to add_filter though - otherwise I get a nasty error of a non-existent function.
    – Howdy_McGee
    Jan 30, 2014 at 17:50
  • corrected. this will work, give it a try Jan 30, 2014 at 17:52
  • I think is a better way to check for the post type via get_current_screen() as the get-param from the url.
    – bueltge
    Jan 31, 2014 at 14:03
  • 1
    i think get_current_screen() will not work because image uploaded by ajax from post edit screen, so get_current_screen will not applicable and return null. Jan 31, 2014 at 14:12
0

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/

1
  • note that my testing was all using the featured image upload, and this may vary if you are using a different method to attach your image to the custom post. Jan 30, 2014 at 20:14
-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.

2
  • I don't think plugins should be considered answers, since plugins are technically off topic here. I'll add that to my questions. Thanks though!
    – Howdy_McGee
    Jan 30, 2014 at 17:17
  • That's ok. This plugin generates code and you paste it in your theme and regenerates the sizes, then you could just delete the plugin. That's why I thought it could be useful. :) good luck! Jan 30, 2014 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.