1

For a custom plugin i am using WP's media-uploader. How can i alter the amount of different image sizes being stored? At the moment the theme uses 5 different image sizes and for my plugin i only need 2 store two image sizes?

Regards,

2

Additional image sizes for uploaded images are created by wp_generate_attachment_metadata() after the file has been uploaded. An array of available sizes is populated using get_intermediate_image_sizes() (thumbnail, medium, large + custom sizes) which is later used to create the images.

Both of the above functions have filters for overriding the sizes available to WordPress during this task, intermediate_image_sizes_advanced and intermediate_image_sizes respectively. By hooking on the any of those filters you should be able to remove the sizes you don't need. I would suggest using intermediate_image_sizes as it is executed earlier and you'll only have to filter an array of strings (image size identifiers).

add_filter('intermediate_image_sizes', 'wpse_101197_image_sizes');
function wpse_101197_image_sizes($sizes) {

    if (($key = array_search('size_1', $sizes)) !== false) {
        unset($sizes[$key]);
    }

    return $sizes;
}

Not however that it is key to only apply this filter when you are handling uploads from within your plugin, naturally, as you will otherwise end up removing images sizes from WordPress globally. Thus, don't forget to remove the filter once you're done with the upload. Also keep in mind that WordPress kind of expects each image to be available in every image size - if a specific size is not found for an image, you will end up with the largest one.

  • thanks. I didn't get your solution to work, but you did point me in the right direction. I altered your function by just returning the image sizes i want. $sizes = array('thumbnail'); return $sizes; I am stuck with applying this filter only to my plugin. Will ask a new question – alex May 30 '13 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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