wp_filter_content_tags() – Filters specific tags in post content and modifies their markup.
Description
Filters specific tags in post content and modifies their markup.
Modifies HTML tags in post content to include new browser and HTML technologies that may not have existed at the time of post creation. These modifications currently include adding srcset`,
HTML tags, as well as adding sizes`, and `loading
attributes to `imgloading
attributes to iframe
HTML tags. Future similar optimizations should be added/expected here.
Usage
$string = wp_filter_content_tags( $content, $context );
Parameters
- $content
- ( string ) required – The HTML content to be filtered.
- $context
- ( string ) optional – Optional. Additional context to pass to the filters. Defaults to
current_filter()
when not set.
Returns
string Converted content with images modified.
Source
File name: wordpress/wp-includes/media.php
Lines:
1 to 100 of 123
function wp_filter_content_tags( $content, $context = null ) { if ( null === $context ) { $context = current_filter(); } $add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context ); 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; case 'iframe': $iframes[ $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] ]; // Add 'width' and 'height' attributes if applicable. if ( $attachment_id > 0 && ! str_contains( $filtered_image, ' width=' ) && ! str_contains( $filtered_image, ' height=' ) ) { $filtered_image = wp_img_tag_add_width_and_height_attr( $filtered_image, $context, $attachment_id ); } // Add 'srcset' and 'sizes' attributes if applicable. if ( $attachment_id > 0 && ! str_contains( $filtered_image, ' srcset=' ) ) { $filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id ); } // Add loading optimization attributes if applicable. $filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); // Add 'decoding=async' attribute unless a 'decoding' attribute is already present. if ( ! str_contains( $filtered_image, ' decoding=' ) ) { $filtered_image = wp_img_tag_add_decoding_attr( $filtered_image, $context ); } /** * Filters an img tag within the content for a given context. * * @since 6.0.0 * * @param string $filtered_image Full img tag with attributes that will replace the source img tag. * @param string $context Additional context, like the current filter name or the function name from where this was called. * @param int $attachment_id The image attachment ID. May be 0 in case the image is not an attachment. */ $filtered_image = apply_filters( 'wp_content_img_tag', $filtered_image, $context, $attachment_id ); if ( $filtered_image !== $match[0] ) { $content = str_replace( $match[0], $filtered_image, $content ); } /* * Unset image lookup to not run the same logic again unnecessarily if the same image tag is used more than * once in the same blob of content. */ unset( $images[ $match[0] ] ); }
Called by
Invoked by
Calls
1 to 7 of 7
- str_contains() – Polyfill for `str_contains()` function added in PHP 8.0.
- wp_filter_content_tags() – Filters specific tags in post content and modifies their markup.
- wp_iframe_tag_add_loading_attr() – Adds `loading` attribute to an `iframe` HTML tag.
- wp_img_tag_add_decoding_attr() – Adds `decoding` attribute to an `img` HTML tag.
- wp_img_tag_add_srcset_and_sizes_attr() – Adds `srcset` and `sizes` attributes to an existing `img` HTML tag.
- wp_img_tag_add_width_and_height_attr() – Adds `width` and `height` attributes to an `img` HTML tag.
- wp_lazy_loading_enabled() – Determines whether to add the `loading` attribute to the specified tag in the specified context.
Call hooks
1 to 1 of 1