Skip to content

Commit 8c374a5

Browse files
Media: ensure wp_get_attachment_image uses valid user-provided width and height.
Fix a bug introduced in WordPress 6.8.2 (r60415) that led to user supplied values for width and height in the $attr array passed to `wp_get_attachment_image` to be overwritten. Props rainbowgeek, ocean90, rollybueno, shreya0shrivastava, heybran, mukesh27. Fixes #63714. git-svn-id: https://develop.svn.wordpress.org/trunk@60641 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 66a9fe9 commit 8c374a5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/wp-includes/media.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,9 +1091,17 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
10911091
*/
10921092
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
10931093

1094-
$attr = wp_parse_args( $attr, $default_attr );
1095-
$attr['width'] = $width;
1096-
$attr['height'] = $height;
1094+
$attr = wp_parse_args( $attr, $default_attr );
1095+
1096+
// Ensure that the `$width` doesn't overwrite an already valid user-provided width.
1097+
if ( ! isset( $attr['width'] ) || ! is_numeric( $attr['width'] ) ) {
1098+
$attr['width'] = $width;
1099+
}
1100+
1101+
// Ensure that the `$height` doesn't overwrite an already valid user-provided height.
1102+
if ( ! isset( $attr['height'] ) || ! is_numeric( $attr['height'] ) ) {
1103+
$attr['height'] = $height;
1104+
}
10971105

10981106
$loading_optimization_attr = wp_get_loading_optimization_attributes(
10991107
'img',

tests/phpunit/tests/media.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,26 @@ static function ( $args ) {
16341634
$this->assertStringContainsString( 'height="150"', $output, 'Height should not be changed.' );
16351635
}
16361636

1637+
/**
1638+
* Test that `wp_get_attachment_image` doesn't overwrite an already valid user-provided width and height.
1639+
*
1640+
* @ticket 63714
1641+
*/
1642+
public function test_wp_get_attachment_image_not_overwrite_user_provided_width_height() {
1643+
$img = wp_get_attachment_image(
1644+
self::$large_id,
1645+
'large',
1646+
false,
1647+
array(
1648+
'width' => 999,
1649+
'height' => 999,
1650+
)
1651+
);
1652+
1653+
$this->assertStringContainsString( 'width="999"', $img, 'User-provided width should not be changed.' );
1654+
$this->assertStringContainsString( 'height="999"', $img, 'User-provided height should not be changed.' );
1655+
}
1656+
16371657
/**
16381658
* Test that `wp_get_attachment_image()` returns a proper alt value.
16391659
*

0 commit comments

Comments
 (0)