Fix: Store scaled image URL for file fields when WP 5.3+ big image scaling is active#2931
Fix: Store scaled image URL for file fields when WP 5.3+ big image scaling is active#2931masteradhoc wants to merge 3 commits intoAutomattic:trunkfrom
Conversation
|
Hi, regarding of handling (very) large images in WP 5.3+
Why is this needed? The idea in WP was to "ignore" as much as possible the uploaded file as it is not suitable for use on the internet. These are typically photos with dimensions larger than 2560px and sizes over 4MB. This type of images shouldn't be used on the internet however WP still stores them just in case the authors may need them later. They are exposed for downloading on the attachment screen in WP. If the same thing is needed somewhere in the plugin maybe it would be better to link to there? |
|
Hi @azaozz, thanks for looking at this! The problem is that WPJM currently stores the original (pre-scaling) URL in post meta, because create_attachment() was called after update_post_meta(). This means:
This PR fixes both by ensuring wp_generate_attachment_metadata() runs before the URL is persisted to post meta, and then using wp_get_attachment_url() to retrieve the canonical (potentially scaled) URL that WordPress itself recorded — keeping everything consistent with how WP handles the attachment internally. |
Fixes #1973
Changes Proposed in this Pull Request
Added an optional
&$actual_urlby-reference parameter tocreate_attachment().When WordPress 5.3+ scales a large image during
wp_generate_attachment_metadata(),the metadata will include
original_imageand_wp_attached_filewill point to the-scaledversion.actual_urlis set viawp_get_attachment_url()in that case,giving callers the canonical URL WordPress has stored.
Moved attachment creation inline within the field loop in
update_job_data(), beforeupdate_post_meta()is called. This ensureswp_generate_attachment_metadata()runsfirst, so any scaled URL is resolved before being persisted to post meta — fixing both
the served image (full-res was used instead of scaled) and
attachment_url_to_postid()failures (stored URL didn't match the scaled URL WordPress recorded internally).
Testing Instructions
submission form.
-scaledversion of the image, notthe original.
attachment_url_to_postid()returns the correct attachment ID for the stored URL.Release Notes
WordPress 5.3+ big image scaling was active, causing oversized images to be served and
breaking attachment ID lookups.
New or Updated Hooks and Templates
Deprecated Code
Screenshot / Video