Repository navigation
Media: Avoid undefined array key warnings in wp_image_src_get_dimensions() - #14863
abhishekfdd wants to merge 1 commit into
Conversation
…ons(). When the attachment meta for a full size image has a `file` key but is missing `width` or `height` (for example, metadata written by SVG plugins, importers, or custom upload handling), the function read those keys unguarded, raising "Undefined array key" warnings and returning `array( 0, 0 )`. The full size check now also requires `width` and `height` to be set, so the function returns `false` in this case, as its documented return value specifies.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
wp_image_src_get_dimensions()reads$image_meta['width']and$image_meta['height']in its full size branch without checking that they exist. When attachment meta has afilekey but lacks dimensions (metadata written by SVG plugins, importers, or custom upload handling), this triggers "Undefined array key" warnings and the function returnsarray( 0, 0 ).This PR requires
widthandheightto be set before taking the full size branch, so the function returnsfalsein this case, matching its documented return value.Note that stock core doesn't produce this meta shape. For SVGs, core stores only
filesize, so the branch isn't reached on a plain upload. The bug shows up with plugin or import written meta.Impact
wp_image_add_srcset_and_sizes()andwp_img_tag_add_width_and_height_attr(), already rejectedarray( 0, 0 ).wp_image_src_get_dimensionsfilter now receivesfalseinstead of a misleading[0, 0].wp_calculate_image_srcset_metafilter, which it previously reached with zero dimensions.This keeps the function's existing contract, same approach as #14861 for #57813, which guards the reads in
wp_get_missing_image_subsizes()without changing its return value.Testing
New test class
Tests_Media_wpImageSrcGetDimensions:false.widthandheightstill returns the correct dimensions.Manual A/B on trunk vs this branch: with meta set to
filewithout dimensions, trunk logs warnings at media.php:1825/1826 on bothwp_get_attachment_image()and a front end Image block render. This branch is silent. Rendered<img>markup is identical on both.Commands:
All pass.
phpcsshows no new issues.Trac ticket: https://core.trac.wordpress.org/ticket/62312
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Local environment setup, implementing the fix, writing unit tests, verifying callers, and running the manual A/B reproduction; the changes were reviewed by me.