WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 6 months ago

#48194 new defect (bug)

Unregistered image sizes used for IMG tag "srcset"

Reported by: galbaras Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords:
Focuses: Cc:

Description

Following design changes, previously-generated thumbnails remain on the site, along with matching post meta entries linked to the respective attachment. WordPress then uses those to produce IMG tags.

In fact, these thumbnails are referenced even when the files don't exist on the site anymore, following a cleanup.

I think WordPress should only use thumbnail URLs for registered image sizes.

For bonus points, there can be a way to remove unregistered size references from attachment meta, perhaps in a Media > Audit admin page, which lets the user decide what to remove and what to keep.

For context, I've just cleaned images on a site and found a lot of legacy references to thumbnails of previously registered sizes in IMG tags. Media management and cleanup plugins I used could only detect the redundant meta data when I re-registered the old sizes temporarily.

Change History (3)

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


6 months ago

#2 @joemcgill
6 months ago

Hi @galbaras. Thanks for reporting this issue.

WordPress makes an assumption that if an image size is present in attachment metadata, then the file is available to be served to user requests and can be included in srcset attributes. The best way to clean up unused image sizes from the database is to regenerate thumbnails after changing themes. You can use a plugin like https://wordpress.org/plugins/regenerate-thumbnails/ or the wp media regenerate WP-CLI command.

Does that help?

#3 @galbaras
6 months ago

Hi @joemcgill ,

I use a different plugin to regenerate thumbnails (Image Regenerate & Select Crop), after having problems with the one you've suggested and getting no support. Alas, no WP-CLI (shared hosting).

At my request, the author of IRSC (@iulia-cazan) added a feature to delete orphaned image meta, which I've used since starting this ticket.

As for assumptions, plugins aren't perfect, and people sometimes make changes outside of WordPress, so turns out this assumption is dangerous.

Since WordPress already runs cron jobs, this image meta audit can be very gradual and run monthly or less, but I still think it will be good practice.

Honestly, I've had a lot to do with image handling and optimisation in WordPress lately and this module may need some rethinking. For example, old embeds lack the "wp-image-XXXX" class, and there's nothing in WordPress to bring them up to date, so no srcset :(

I've written a plugin for doing this last bit, and am happy to share my code (works great, but will need some serious cleanup).

But this isn't the only problem. Thumbnail regeneration must be integrated with image optimisation, image file renaming and other functions, and WordPress doesn't facilitate this well enough, it turns out, so the plugin authors have to collaborate directly, rather than work through a good medium.

Cheers,
Gal

Note: See TracTickets for help on using tickets.