Skip to content

Commit 4014d16

Browse files
Media: expose height and width attributes to the 'wp_get_attachment_image_attributes' filter.
Include the image height and width in the attributes passed to the 'wp_get_attachment_image_attributes' filter. Developers can use this to adjust the width and height attributes returned from the 'wp_get_attachment_image_attributes' function. Props divinenephron, nacin, Sam_a, wpsmith, anatolbroder, ericlewis, puggan, SergeyBiryukov, spacedmonkey, adamsilverstein, flixos90, sandeepdahiya, SirLouen. Fixes #14110. git-svn-id: https://develop.svn.wordpress.org/trunk@60415 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3045051 commit 4014d16

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/wp-includes/media.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,6 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
10701070
list( $src, $width, $height ) = $image;
10711071

10721072
$attachment = get_post( $attachment_id );
1073-
$hwstring = image_hwstring( $width, $height );
10741073
$size_class = $size;
10751074

10761075
if ( is_array( $size_class ) ) {
@@ -1090,15 +1089,14 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
10901089
*
10911090
* @param string $context The context. Default 'wp_get_attachment_image'.
10921091
*/
1093-
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
1094-
$attr = wp_parse_args( $attr, $default_attr );
1092+
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
1093+
$attr = wp_parse_args( $attr, $default_attr );
1094+
$attr['width'] = $width;
1095+
$attr['height'] = $height;
10951096

1096-
$loading_attr = $attr;
1097-
$loading_attr['width'] = $width;
1098-
$loading_attr['height'] = $height;
10991097
$loading_optimization_attr = wp_get_loading_optimization_attributes(
11001098
'img',
1101-
$loading_attr,
1099+
$attr,
11021100
$context
11031101
);
11041102

@@ -1169,8 +1167,16 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
11691167
*/
11701168
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
11711169

1172-
$attr = array_map( 'esc_attr', $attr );
1173-
$html = rtrim( "<img $hwstring" );
1170+
if ( isset( $attr['height'] ) && is_numeric( $attr['height'] ) ) {
1171+
$height = absint( $attr['height'] );
1172+
}
1173+
if ( isset( $attr['width'] ) && is_numeric( $attr['width'] ) ) {
1174+
$width = absint( $attr['width'] );
1175+
}
1176+
unset( $attr['height'], $attr['width'] );
1177+
$attr = array_map( 'esc_attr', $attr );
1178+
$hwstring = image_hwstring( $width, $height );
1179+
$html = rtrim( "<img $hwstring" );
11741180

11751181
foreach ( $attr as $name => $value ) {
11761182
$html .= " $name=" . '"' . $value . '"';

tests/phpunit/tests/media.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,53 @@ public function test_wp_get_attachment_image_filter_output() {
15831583
$this->assertSame( $expected, $output );
15841584
}
15851585

1586+
/**
1587+
* @ticket 14110
1588+
*/
1589+
public function test_wp_get_attachment_image_filter_with_height_width() {
1590+
$mock_action = new MockAction();
1591+
add_filter( 'wp_get_attachment_image_attributes', array( $mock_action, 'filter' ) );
1592+
wp_get_attachment_image( self::$large_id );
1593+
$args = $mock_action->get_args();
1594+
$this->assertArrayHasKey( '0', $args, 'First argument should be an array.' );
1595+
$this->assertArrayHasKey( '0', $args[0], 'First argument should be an array.' );
1596+
$this->assertArrayHasKey( 'width', $args[0][0], 'Width should be set.' );
1597+
$this->assertArrayHasKey( 'height', $args[0][0], 'Height should be set.' );
1598+
}
1599+
1600+
/**
1601+
* @ticket 14110
1602+
*/
1603+
public function test_wp_get_attachment_image_filter_change_height_width() {
1604+
add_filter(
1605+
'wp_get_attachment_image_attributes',
1606+
static function ( $args ) {
1607+
$args['height'] = '999';
1608+
$args['width'] = '999';
1609+
return $args;
1610+
}
1611+
);
1612+
$output = wp_get_attachment_image( self::$large_id );
1613+
$this->assertStringContainsString( 'width="999"', $output, 'Width should be changed.' );
1614+
$this->assertStringContainsString( 'height="999"', $output, 'Height should be changed.' );
1615+
}
1616+
1617+
/**
1618+
* @ticket 14110
1619+
*/
1620+
public function test_wp_get_attachment_image_filter_unset_height_width() {
1621+
add_filter(
1622+
'wp_get_attachment_image_attributes',
1623+
static function ( $args ) {
1624+
unset( $args['height'], $args['width'] );
1625+
return $args;
1626+
}
1627+
);
1628+
$output = wp_get_attachment_image( self::$large_id );
1629+
$this->assertStringContainsString( 'width="150"', $output, 'Width should not be changed.' );
1630+
$this->assertStringContainsString( 'height="150"', $output, 'Height should not be changed.' );
1631+
}
1632+
15861633
public function filter_wp_get_attachment_image() {
15871634
return 'Override wp_get_attachment_image';
15881635
}

0 commit comments

Comments
 (0)