63714 - Ensure wp_get_attachment_image() doesn't overwrite an already valid user-provided width and height#9315
63714 - Ensure wp_get_attachment_image() doesn't overwrite an already valid user-provided width and height#9315heybran wants to merge 2 commits intoWordPress:trunkfrom heybran:fix/svg-image-width-height-attributes-lost
wp_get_attachment_image() doesn't overwrite an already valid user-provided width and height#9315Conversation
Ensure `wp_get_attachment_image()` doesn't overwrite an already valid user-provided width and height.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the 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. |
|
Hi @heybran! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
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. |
src/wp-includes/media.php
Outdated
| $attr = wp_parse_args( $attr, $default_attr ); | ||
|
|
||
| // Ensure that the `$width` doesn't overwrite an already valid user-provided width. | ||
| if ( is_numeric( $width ) && ( ! isset( $attr['width'] ) || ! is_numeric( $attr['width'] ) ) ) { |
There was a problem hiding this comment.
Let's check isset and numeric for $attr['width'] then numeric for $width as it always have numeric value.
| if ( is_numeric( $width ) && ( ! isset( $attr['width'] ) || ! is_numeric( $attr['width'] ) ) ) { | |
| if ( ( ! isset( $attr['width'] ) || ! is_numeric( $attr['width'] ) && is_numeric( $width ) ) ) { |
Do same for height also.
There was a problem hiding this comment.
Thanks for the review. I found $width and $height can be null in two of our projects when we're debugging the missing width/height attributes the other day, only for SVG images.
But I think in this case, if I'm not wrong, there is no need to check is_numeric( $width ) at all, checking ! isset( $attr['width'] ) || ! is_numeric( $attr['width'] ) alone should be enough here. I will take another closer look tomorrow. And I also wait and see if Adam has any further feedback.
There was a problem hiding this comment.
You're right about the $width and $height always have numeric value, I tracked down the codes to set the width and height.
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/media.php#L480-L482
The reason that I got null for the $width and $height is due to a filter we're using internally in the projects at the company that I work at. So all good there, thanks again.
| public function test_wp_get_attachment_image_not_overwrite_user_provided_width_height() { | ||
| [$src, $width, $height] = wp_get_attachment_image_src( self::$large_id, 'large' ); | ||
|
|
||
| $user_provided_width = is_numeric( $width ) ? (int) $width + 1 : 999; | ||
| $user_provided_height = is_numeric( $height ) ? (int) $height + 1 : 199; | ||
|
|
||
| $img = wp_get_attachment_image( | ||
| self::$large_id, | ||
| 'large', | ||
| false, | ||
| array( | ||
| 'width' => $user_provided_width, | ||
| 'height' => $user_provided_height, | ||
| ) | ||
| ); | ||
|
|
||
| $expected_width_attribute = 'width="' . $user_provided_width . '"'; | ||
| $expected_height_attribute = 'height="' . $user_provided_height . '"'; | ||
| $this->assertStringContainsString( $expected_width_attribute, $img, 'User-provided width should not be changed.' ); | ||
| $this->assertStringContainsString( $expected_height_attribute, $img, 'User-provided height should not be changed.' ); | ||
| } |
There was a problem hiding this comment.
| public function test_wp_get_attachment_image_not_overwrite_user_provided_width_height() { | |
| [$src, $width, $height] = wp_get_attachment_image_src( self::$large_id, 'large' ); | |
| $user_provided_width = is_numeric( $width ) ? (int) $width + 1 : 999; | |
| $user_provided_height = is_numeric( $height ) ? (int) $height + 1 : 199; | |
| $img = wp_get_attachment_image( | |
| self::$large_id, | |
| 'large', | |
| false, | |
| array( | |
| 'width' => $user_provided_width, | |
| 'height' => $user_provided_height, | |
| ) | |
| ); | |
| $expected_width_attribute = 'width="' . $user_provided_width . '"'; | |
| $expected_height_attribute = 'height="' . $user_provided_height . '"'; | |
| $this->assertStringContainsString( $expected_width_attribute, $img, 'User-provided width should not be changed.' ); | |
| $this->assertStringContainsString( $expected_height_attribute, $img, 'User-provided height should not be changed.' ); | |
| } | |
| public function test_wp_get_attachment_image_not_overwrite_user_provided_width_height() { | |
| $img = wp_get_attachment_image( | |
| self::$large_id, | |
| 'large', | |
| false, | |
| array( | |
| 'width' => 999, | |
| 'height' => 999, | |
| ) | |
| ); | |
| $this->assertStringContainsString( 'width="999"', $img, 'User-provided width should not be changed.' ); | |
| $this->assertStringContainsString( 'height="999"', $img, 'User-provided height should not be changed.' ); | |
| } |
Simplified
|
Thanks @heybran for the PR. I’ve left some feedback and suggestions. Please take a look and let me know if you need any clarification or further details. |
|
@adamsilverstein Could you please take a look when you have time? |
|
Looks good to me - thanks for the PR @heybran and the review @mukeshpanchal27! |
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Thanks @heybran for the changes. Looks good to me.
|
Hey @ajayadav09 - thanks for the feedback. Would you be able to create a new trac ticket for this so it doesn't get lost? It looks like it might be a separate issue. Also, if possible please share the SVG that is causing the issue. |
|
Hi @ajayadav09, Thanks for the feedback. I don't think this is a bug as the The codes for default size of |
|
@adamsilverstein @heybran thank you for your response and thank you for pointing me to the right direction. Yes I realised the idea to put a custom sized logo in the icon attribute of the button component was not an ideal implementaion, as the icon without assigned width and height is set to 24px. I will close the ticket I had created. |



Trac ticket: https://core.trac.wordpress.org/ticket/63714
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.