I have image assets that I load externally and insert programmatically. There are 3 different asset classes:

 landscape, portrait, and 1:1

The default image_sizes are creating a lot of clutter that's of no use to me. So I've disabled those and added my own custom ones.

But Wordpress is generating resized assets that fits every asset class, despite it only being relevant for 1/3rd. Since the only condition for making a resized asset seems to be exceeding a width/height threshold.

Is it possible to indicate which specific image sizes you want Wordpress to create when you insert an image asset at file_put_contents or wp_update_attachment_metadata, specifically for this one asset, instead of dictating the sizes that should be created globally? Such that we only create the extra sizes relevant to landscape assets when we know we're insert landscape assets?

Or alternatively, can you create a custom image_size that factors in aspect ratio and not just raw pixel dimensions?

1 Answer 1


Right before image subsizes are created, there's a filter called intermediate_image_sizes_advanced that lets you modify the list of sizes.

Here's code that you could expand on — right now it just removes the thumbnail as a proof of concept:

function remove_image_sizes_before_generation( $new_sizes, $image_meta, $attachment_id ) {
    $width = $image_meta[0];
    $height = $image_meta[1];

    // ... Calculate aspect ratio and remove the image sizes that aren't relevant
    unset( $new_sizes['thumbnail'] );

    return $new_sizes;

add_filter( 'intermediate_image_sizes_advanced', 'remove_image_sizes_before_generation', 10 , 3 );

The nice thing about doing it this way is that the image will then "remember" what sizes it doesn't have, even if they are registered sizes — for example, it won't show up in drop-downs where you insert the image.

enter image description here

  • This is perfect! Now I can actually leave the default sizes alone and just remove them on a case-by-case basis! Instead of using aspect ratio I'm using a unique destination path to identify when certain sizes should be active or not. Which works here just fine. But I noticed that $image_meta["image_meta"] exists, with "keywords", "title", "credit" and other things in it. Do you know if it's possible to insert things here, that one can use to identify what sizes an image should be rendered in?
    – Fluxian
    Sep 21, 2022 at 16:57
  • Yes, it looks like you could use wp_update_attachment_metadata( $attachment_id, $image_meta ) — with $image_meta being the full data set of meta values? Sep 21, 2022 at 17:24
  • Ah but won't updating pre-existing attachments happen after intermediate_image_sizes_advanced has already fired?
    – Fluxian
    Sep 21, 2022 at 17:33
  • I'm probably not understanding exactly when you need to add the $image_meta data — but in the intermediate_image_sizes_advanced hook, you'd have to use wp_update_attachment_metadata() to make changes. Sep 21, 2022 at 18:49
  • Yeah it doesn't matter :D I wanted to insert some metadata so I could tell which category of image_sizes this particular image belongs to within intermediate_image_sizes_advanced, instead of finding that out by what folder it's placed it
    – Fluxian
    Sep 21, 2022 at 18:51

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.