[wp-trac] [WordPress Trac] #55347: There should be filter for image tag in wp_filter_content_tags()

WordPress Trac noreply at wordpress.org
Thu Mar 31 23:24:23 UTC 2022


#55347: There should be filter for image tag in wp_filter_content_tags()
---------------------------------------------+-----------------------
 Reporter:  pbearne                          |       Owner:  flixos90
     Type:  enhancement                      |      Status:  closed
 Priority:  normal                           |   Milestone:  6.0
Component:  Media                            |     Version:
 Severity:  normal                           |  Resolution:  fixed
 Keywords:  has-patch has-unit-tests commit  |     Focuses:
---------------------------------------------+-----------------------

Comment (by peterwilsoncc):

 I was able to reproduce the issue @superpoincare reports:

 The issue occurs when an identical image tag appears in `the_content`
 twice and either of the following are true:

 * the image includes each of the attributes WP Core adds by default,
 prompting core not to replace them
 * the addition of these custom attributes is disabled via filters

 WP has done this in the core functionality for a while, but it's never
 mattered as the `<img />` tag was only replaced by an `<img />` tag.

 With the filter, a developer may wish to replace the images tag by
 wrapping it in another tag (for example `<picture />` with an `<img />`
 fallback). With the new filter, Core does need to replace the tag once and
 once only.

 I'll create a follow up issue and assign it to @flixos90

 ----

 These are the tests I used to reproduce the issue.

 {{{#!php
 <?php
 /**
  * @ticket 55347
  */
 public function
 test_wp_filter_content_tags_with_identical_images_custom_attributes() {
         $img         = get_image_tag( self::$large_id, '', '', '',
 'medium' );
         $img         = str_replace( '<img ', '<img srcset="custom"
 sizes="custom" loading="custom" ', $img );
         $the_content = "$img\n\n$img";

         add_filter(
                 'wp_content_img_tag',
                 function( $filtered_image ) {
                         return "<span>$filtered_image</span>";
                 }
         );

         $actual = wp_filter_content_tags( $the_content );
         $this->assertStringNotContainsString( '<span><span>', $actual );
 }

 /**
  * @ticket 55347
  */
 public function
 test_wp_filter_content_tags_with_identical_images_disabled_core_filters()
 {
         $img         = get_image_tag( self::$large_id, '', '', '',
 'medium' );
         $the_content = "$img\n\n$img";

         add_filter( 'wp_lazy_loading_enabled', '__return_false' );
         add_filter( 'wp_img_tag_add_width_and_height_attr',
 '__return_false' );
         add_filter( 'wp_img_tag_add_srcset_and_sizes_attr',
 '__return_false' );

         add_filter(
                 'wp_content_img_tag',
                 function( $filtered_image ) {
                         return "<span>$filtered_image</span>";
                 }
         );

         $actual = wp_filter_content_tags( $the_content );
         $this->assertStringNotContainsString( '<span><span>', $actual );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55347#comment:14>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list