admin管理员组

文章数量:1122826

We like to use custom image sizes for custom WP themes. This way, we can provide appropriate images for the custom elements and we can ensure that images don't load too large or too heavy. Now we've encountered a problem; most images are loaded with the 'full' image size, even though the correct image size is present in the uploads folder.

We found that for these images, the generated image sizes are missing from the _wp_attachment_metadata postmeta.

After regenerating the image sizes, the _wp_attachment_metadata for all images contain the "sizes" array correctly. This allows us to display the correct image sizes.

But the "sizes" data disappears after some time and so the "full" image size is displayed again. The period over which this happens seems to be random.

I have found related questions, but none seem to address the core problem. Can anyone explain why this data is missing? Is there a core WordPress functionality that checks the _wp_attachment_metadata for each image in a recurring action? Is something going wrong there?

Related question: Regenerate missing _wp_attachment_metadata

Related question now giving a 404:

Update

Thanks to @TomJNowell I was made aware of the fact that more information is necessary in order to be able to answer my question.

What does the environment look like?

  • The website is build with a WordPress boilerplate from roots.io: Bedrock

  • It contains a multisite setup with currently one website. We use a multisite environment to be able to present the website in multiple languages. For the translation of strings we use the plugin Loco Translate. Only one language is necessary for now.

  • It is a webshop and therefore we use WooCommerce

  • As said, we made a custom WordPress theme containing WooCommerce templates to override the default WooCommerce themes.

  • Plugins used: ACF Pro, Bugsnag, Gravity Forms, Loco Translate, Mollie for WooCommerce, Premmerce Permalink Manager for WooCommerce, WooCommerce, WooCommerce EU VAT Number, WooCommerce PDF Invoices, xCore Rest API extension, Yoast SEO.

  • Beside that we installed "Regenerate thumbnails" to be able to generate the custom image sizes. This plugin is still active because it also fixed the symptoms of our issue. When clicking "regenerate thumbnails" the image sizes were added to the _wp_attachment_metadata again.

  • There is no cleaner plugin active and there never was one active either.

  • We do not use custom code in our theme that has impact on the functionality of the _wp_attachment_metadata, aside from the add_image_size function.

More information about the issue

  • It affects all images.
  • We couldnt detect any "schedule" on which the issue occures. After fixing the symptoms with the plugin "Regenerate Thumbnails" we would get notified by our client that the issue occured again.
  • We do not come across any errors, notifications or any logging about the issue. Bugsnag is also not detecting any issues.
  • The image sizes do exist in the uploads folder, only the meta data is missing.

We fixed the symptoms

For now we fixed the symptoms by running a cronjob on a WP CLI script. The cronjob fires every 3 hours. We use: wp media regenerate We installed the script and the cronjob after the issue was detected.

The script:

#!/bin/bash

WPPATH="/home/example/domains/example.nl/public_html/web/wp"

for URL in = $(/usr/local/bin/wp site list --fields=url --format=csv --path="$WPPATH")
do
    /usr/local/bin/wp media regenerate --only-missing --url="$URL" --path="$WPPATH" --yes --quiet
done

We like to use custom image sizes for custom WP themes. This way, we can provide appropriate images for the custom elements and we can ensure that images don't load too large or too heavy. Now we've encountered a problem; most images are loaded with the 'full' image size, even though the correct image size is present in the uploads folder.

We found that for these images, the generated image sizes are missing from the _wp_attachment_metadata postmeta.

After regenerating the image sizes, the _wp_attachment_metadata for all images contain the "sizes" array correctly. This allows us to display the correct image sizes.

But the "sizes" data disappears after some time and so the "full" image size is displayed again. The period over which this happens seems to be random.

I have found related questions, but none seem to address the core problem. Can anyone explain why this data is missing? Is there a core WordPress functionality that checks the _wp_attachment_metadata for each image in a recurring action? Is something going wrong there?

Related question: Regenerate missing _wp_attachment_metadata

Related question now giving a 404: https://wordpress.stackexchange.com/questions/330267/attachment-metadata-randomly-missing

Update

Thanks to @TomJNowell I was made aware of the fact that more information is necessary in order to be able to answer my question.

What does the environment look like?

  • The website is build with a WordPress boilerplate from roots.io: Bedrock

  • It contains a multisite setup with currently one website. We use a multisite environment to be able to present the website in multiple languages. For the translation of strings we use the plugin Loco Translate. Only one language is necessary for now.

  • It is a webshop and therefore we use WooCommerce

  • As said, we made a custom WordPress theme containing WooCommerce templates to override the default WooCommerce themes.

  • Plugins used: ACF Pro, Bugsnag, Gravity Forms, Loco Translate, Mollie for WooCommerce, Premmerce Permalink Manager for WooCommerce, WooCommerce, WooCommerce EU VAT Number, WooCommerce PDF Invoices, xCore Rest API extension, Yoast SEO.

  • Beside that we installed "Regenerate thumbnails" to be able to generate the custom image sizes. This plugin is still active because it also fixed the symptoms of our issue. When clicking "regenerate thumbnails" the image sizes were added to the _wp_attachment_metadata again.

  • There is no cleaner plugin active and there never was one active either.

  • We do not use custom code in our theme that has impact on the functionality of the _wp_attachment_metadata, aside from the add_image_size function.

More information about the issue

  • It affects all images.
  • We couldnt detect any "schedule" on which the issue occures. After fixing the symptoms with the plugin "Regenerate Thumbnails" we would get notified by our client that the issue occured again.
  • We do not come across any errors, notifications or any logging about the issue. Bugsnag is also not detecting any issues.
  • The image sizes do exist in the uploads folder, only the meta data is missing.

We fixed the symptoms

For now we fixed the symptoms by running a cronjob on a WP CLI script. The cronjob fires every 3 hours. We use: wp media regenerate We installed the script and the cronjob after the issue was detected.

The script:

#!/bin/bash

WPPATH="/home/example/domains/example.nl/public_html/web/wp"

for URL in = $(/usr/local/bin/wp site list --fields=url --format=csv --path="$WPPATH")
do
    /usr/local/bin/wp media regenerate --only-missing --url="$URL" --path="$WPPATH" --yes --quiet
done
Share Improve this question edited Dec 2, 2022 at 10:40 braam asked Nov 30, 2022 at 14:21 braambraam 12 bronze badges 4
  • 1 do you have "cleaner" type plugins or tools that try to cleanup/repair/delete expired or unused data? This data doesn't just delete itself in a normal WP install. Also even if you find people with the same outcome, there's no guarantee they had the same cause/solution. Anybody trying to answer this will need significantly more information about how your site is structured, the software, etc. You'll also need to be much more specific in your description, which images? How long does it take for the data to go missing? Is it all images or some? Is it the same amount of time? Logging? – Tom J Nowell Commented Nov 30, 2022 at 15:58
  • Thanks @TomJNowell! I updated my question with all the information I thought would helpfull. – braam Commented Dec 2, 2022 at 10:41
  • you may want to ask your host if they do any kind of cleanup or optimisation at their end as part of their service, likewise you can add code that logs when metadata with that meta key name is deleted and print a stack trace revealing the plugin/theme code responsible. This isn't something WP deletes unless the attachment is being deleted which is not what you're describing – Tom J Nowell Commented Dec 2, 2022 at 13:18
  • Thanks again Tom. I've added logging on the following actions: add_post_meta, update_postmeta & delete_post_meta. And only do so when the meta_key is equal to: "_wp_attachment_metadata". Inside the log functionality I use the wp_debug_backtrace_summary() function to be able to print a stacktrace. If anything usefull comes up I will update this thread. – braam Commented Dec 8, 2022 at 9:22
Add a comment  | 

1 Answer 1

Reset to default 0

After logging the actions add_post_meta, update_postmeta & delete_post_meta we found out that WooCommerce calls the filter woocommerce_resize_images when the function wp_get_attachment_image_src was used. We use that function abundantly in our themes.

I'm not sure why this filter is an issue but deactivating it resolved the problem.

For anyone finding this issue. Try adding this to your theme: add_filter('woocommerce_resize_images', '__return_false');

本文标签: theme developmentAfter a while attachment meta data is missing quotsizesquot data