Make WordPress Core


Ignore:
Timestamp:
07/07/2025 05:07:55 PM (6 months ago)
Author:
audrasjb
Message:

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.

Reviewed by audrasjb.
Merges [60415] to the 6.8 branch.
Props divinenephron, nacin, Sam_a, wpsmith, anatolbroder, ericlewis, puggan, SergeyBiryukov, spacedmonkey, adamsilverstein, flixos90, sandeepdahiya, SirLouen.
Fixes #14110.

Location:
branches/6.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.8

  • branches/6.8/tests/phpunit/tests/media.php

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