Make WordPress Core

Changeset 53028


Ignore:
Timestamp:
03/29/2022 11:56:18 PM (3 years ago)
Author:
flixos90
Message:

Media: Introduce wp_content_img_tag filter.

This filter allows modifying individual img tags within a blob of content that are by default processed by the wp_filter_content_tags() function. The addition of this filter facilitates plugins that tweak images to accomplish this goal without re-implementing duplicate content image parser logic, which furthermore can have a negative performance impact due to additional regular expressions.

In addition to the filterable img tag, the filter receives the context (typically the function or filter in which the content is parsed) and the attachment ID. The latter may be 0, in case the image is not an attachment (for example when it is an external image URL).

Props adamsilverstein, flixos90, pbearne, peterwilsoncc.
Fixes #55347.

Location:
trunk
Files:
2 edited

Legend:

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

    r52957 r53028  
    18431843                $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context );
    18441844            }
     1845
     1846            /**
     1847             * Filters an img tag within the content for a given context.
     1848             *
     1849             * @since 6.0.0
     1850             *
     1851             * @param string $filtered_image Full img tag with attributes that will replace the source img tag.
     1852             * @param string $context        Additional context, like the current filter name or the function name from where this was called.
     1853             * @param int    $attachment_id  The image attachment ID. May be 0 in case the image is not an attachment.
     1854             */
     1855            $filtered_image = apply_filters( 'wp_content_img_tag', $filtered_image, $context, $attachment_id );
    18451856
    18461857            if ( $filtered_image !== $match[0] ) {
  • trunk/tests/phpunit/tests/media.php

    r52190 r53028  
    22952295
    22962296    /**
     2297     * @ticket 55347
     2298     */
     2299    public function test_wp_filter_content_tags_has_filter() {
     2300        $filter = new MockAction();
     2301        add_filter( 'wp_content_img_tag', array( &$filter, 'filter' ) );
     2302        $img_tag_1 = get_image_tag( self::$large_id, '', '', '', 'medium' );
     2303
     2304        wp_filter_content_tags( $img_tag_1 );
     2305        $this->assertSame( 1, $filter->get_call_count() );
     2306    }
     2307    /**
    22972308     * @ticket 33641
    22982309     * @ticket 34528
Note: See TracChangeset for help on using the changeset viewer.