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

WordPress Trac noreply at wordpress.org
Tue Mar 15 23:50:21 UTC 2022


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

Comment (by pbearne):

 Without this filter, you have to duplicate the regex code from WP core and
 create a function like this

 {{{#!php
 <?php
         public function filter_content_tags( $content, $context = null ) {
                 if ( null === $context ) {
                         $context = current_filter();
                 }

                 if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/',
 $content, $matches, PREG_SET_ORDER ) ) {
                         return $content;
                 }

                 // List of the unique `img` tags found in $content.
                 $images = array();

                 // List of the unique `iframe` tags found in $content.
                 $iframes = array();

                 foreach ( $matches as $match ) {
                         list( $tag, $tag_name ) = $match;

                         switch ( $tag_name ) {
                                 case 'img':
                                         if ( preg_match( '/wp-
 image-([0-9]+)/i', $tag, $class_id ) ) {
                                                 $attachment_id = absint(
 $class_id[1] );

                                                 if ( $attachment_id ) {
                                                         // If exactly the
 same image tag is used more than once, overwrite it.
                                                         // All identical
 tags will be replaced later with 'str_replace()'.
                                                         $images[ $tag ] =
 $attachment_id;
                                                         break;
                                                 }
                                         }
                                         $images[ $tag ] = 0;
                                         break;
                         }
                 }

                 // Reduce the array to unique attachment IDs.
                 $attachment_ids = array_unique( array_filter(
 array_values( $images ) ) );

                 if ( count( $attachment_ids ) > 1 ) {
                         /*
                          * Warm the object cache with post and meta
 information for all found
                          * images to avoid making individual database
 calls.
                          */
                         _prime_post_caches( $attachment_ids, false, true
 );
                 }

                 // Iterate through the matches in order of occurrence as
 it is relevant for whether or not to lazy-load.
                 foreach ( $matches as $match ) {
                         // Filter an image match.
                         if ( isset( $images[ $match[0] ] ) ) {
                                 $filtered_image = $match[0];
                                 $attachment_id  = $images[ $match[0] ];
                                 /**
                                  * Filters img tag that will be injected
 into the content.
                                  *
                                  * @param string $filtered_image the img
 tag with attributes being created that will
                                  *
 replace the source img tag in the content.
                                  * @param string $context Optional.
 Additional context to pass to the filters.
                                  *                        Defaults to
 `current_filter()` when not set.
                                  * @param int $attachment_id the ID of the
 image attachment.
                                  *
                                  * @since 1.0.0
                                  *
                                  */
                                 $filtered_image = apply_filters(
 'wp_dominant_color_img_tag_add_adjust', $filtered_image, $context,
 $attachment_id );

                                 if ( $filtered_image !== $match[0] ) {
                                         $content = str_replace( $match[0],
 $filtered_image, $content );
                                 }
                         }
                 }

                 return $content;
         }
 }}}

 In this example, I have added a filtered in the same logical place as the
 requested change and I simple hook into it to add the additional tags to
 the images

 which has to be maintained if core updates the regex it's unlikely that
 plugin authors might miss it

 I hope this makes sense

 Info: We are working on a patch to add an images dominant color
 placeholder to lazy loaded images

 Paul

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


More information about the wp-trac mailing list