WordPress.org

Make WordPress Core

Changeset 49234


Ignore:
Timestamp:
10/20/2020 03:29:42 PM (15 months ago)
Author:
SergeyBiryukov
Message:

Media: Introduce a filter for wp_get_attachment_image() HTML output.

Props prionkor, antpb, donmhico, audrasjb, Mista-Flo, hellofromTonya.
Fixes #50801.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r49223 r49234  
    993993
    994994/**
    995  * Get an HTML img element representing an image attachment
     995 * Get an HTML img element representing an image attachment.
    996996 *
    997997 * While `$size` will accept an array, it is better to register a size with
     
    11001100    }
    11011101
    1102     return $html;
     1102    /**
     1103     * HTML img element representing an image attachment.
     1104     *
     1105     * @since 5.6.0
     1106     *
     1107     * @param string       $html          HTML img element or empty string on failure.
     1108     * @param int          $attachment_id Image attachment ID.
     1109     * @param string|int[] $size          Requested image size. Can be any registered image size name, or
     1110     *                                    an array of width and height values in pixels (in that order).
     1111     * @param bool         $icon          Whether the image should be treated as an icon.
     1112     * @param array        $attr          Array of attribute values for the image markup, keyed by attribute name.
     1113     *                                    See wp_get_attachment_image().
     1114     */
     1115    return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr );
    11031116}
    11041117
  • trunk/tests/phpunit/tests/media.php

    r49193 r49234  
    13421342
    13431343        $this->assertSame( $expected, wp_get_attachment_image( self::$large_id ) );
     1344    }
     1345
     1346    /**
     1347     * @ticket 50801
     1348     */
     1349    function test_wp_get_attachment_image_filter_output() {
     1350        $image    = image_downsize( self::$large_id, 'thumbnail' );
     1351        $expected = 'Override wp_get_attachment_image';
     1352
     1353        add_filter( 'wp_get_attachment_image', array( $this, 'filter_wp_get_attachment_image' ) );
     1354        $output = wp_get_attachment_image( self::$large_id );
     1355        remove_filter( 'wp_get_attachment_image', array( $this, 'filter_wp_get_attachment_image' ) );
     1356
     1357        $this->assertSame( $expected, $output );
     1358    }
     1359
     1360    function filter_wp_get_attachment_image() {
     1361        return 'Override wp_get_attachment_image';
    13441362    }
    13451363
Note: See TracChangeset for help on using the changeset viewer.