1

I have a custom post type with an image upload meta field. Every time I upload it an image with it though, I am getting extra image files for each of the predefined sizes from the theme's functions.php file.

Is there a way to disable these? The method I am currently thinking is this:

$image_sizes = get_image_sizes();

foreach( $image_sizes as $size ) {
   $size_name = "{$size}";
   remove_image_size( $size_name );
}

// Process Upload as Normal

foreach( $image_sizes as $size ) {
   $size_name = "{$size}";
   $size_width = $size['width'];
   $size_height = $size['height'];
   $size_crop = $size['crop'];

   add_image_size( $size_name, $size_width, $size_height, $size_crop );
}

So it will, inside an if() conditional based in upload MIME Type, delete all registered image sizes, process the upload, and then re-add all the image sizes back.

But this seems very clunky and prone to problems cropping up. Especially since this is for a plugin and not a theme.

Is there a hook or something I can use to exclude uploads from a Custom Post Type editor?

0

This is a problematic idea. Wordpress do not have any strong connectivity between an image (or any other uploaded file) and any content (post). This means that in theory you can not make a decisions like if it is an image on page do X as nothing ensures you that the image will not be used somewhere else as well.

This is a wordpress design decision, and frankly, as much as I personally dislike it, there is no point in fighting it as you will have to constantly fight the side effect of it (image regeneration for example) and except for cluttering the upload directories it does not do much damage. The only real solution is wordpress switching to image generation on the fly which is unlikely to happen any time soon.

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.