Support » Developing with WordPress » how to limit the amount of images created by wp_generate_attachment_metadata()

  • Resolved marcnyc

    (@marcnyc)


    In my Media Settings I have 150×150, 300×300 and 800×800 and yet when I use wp_generate_attachment_metadata() I get 18-24 different sized images of the same image. Are all these acqually required? Or is this a waste of server space? And if so, how can I limit it to the 3 or 4 that I need for responsiveness?

    • This topic was modified 1 year, 7 months ago by marcnyc.
    • This topic was modified 1 year, 7 months ago by marcnyc. Reason: wp_generate_attachment_metadata, resize, limit
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Yikes! Your theme and/or plugins have added their own extra image sizes. While it’s possible to remove them after they are registered to limit the number generated, doing so will break any code that is specifically requesting the removed sizes.

    Thread Starter marcnyc

    (@marcnyc)

    I am using the Newspaper X theme but I only just installed it 2 weeks ago so not a lot would break.
    Is there a way I can set that limit so that from now on the future posts will only have 2-3 images instead of 25? Then I can check the posts from the last 2 weeks and adjust

    Moderator bcworkz

    (@bcworkz)

    My concern wasn’t so much in existing posts but with your theme or plugin code which calls for specific sizes. It may not be an issue. I don’t see much harm in trying to remove a size and see what happens. If anything goes wrong, remove the removal code.

    Learn what the image size names are by var_dumping the global $_wp_additional_image_sizes. With the undesired name, call remove_image_size() from an action callback hooked to “init”. Add the callback with a large priority argument. Hopefully it’ll run after the size has already been added (usually from “after_theme_setup” action).

    If these sizes appear in image selection dialogs, the removed size might still be listed. You’d need find how the list was constructed and make appropriate corrections. One way sizes are added to the list is through the “image_size_names_choose” filter. Any filter added can be removed by your own code.

    Before embarking on any such effort, it’d be worth asking in the theme’s dedicated support forum if there’s any easier way to remove undesired sizes.

    BTW, the above mentioned actions would not affect existing images, only newly uploaded images. You can either leave them be, manually delete them, or use a plugin like “regenerate thumbnails”.

    Thread Starter marcnyc

    (@marcnyc)

    I found (in the theme) the settings to prevent the generation of all those extra thumbnails and I will ask if there is a way to remove unwanted/unused ones.
    Thanks for your help

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to limit the amount of images created by wp_generate_attachment_metadata()’ is closed to new replies.