wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_filter_content_tags › WordPress Function
Since5.5.0
Deprecatedn/a
› wp_filter_content_tags ( $content, $context = null )
Parameters: (2) |
|
See: | |
Returns: |
|
Defined at: |
|
Codex: | |
Change Log: |
|
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 addingsrcset
, sizes
, and loading
attributes to img
HTML tags, as well
as adding loading
attributes to iframe
HTML tags.
Future similar optimizations should be added/expected here.Related Functions: wp_filter_object_list, wp_filter_post_kses, filter_block_content, wp_filter_nohtml_kses, the_content_rss
Source
function wp_filter_content_tags( $content, $context = null ) { if ( null === $context ) { $context = current_filter(); } $add_img_loading_attr = wp_lazy_loading_enabled( 'img', $context ); $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 ); } foreach ( $images as $image => $attachment_id ) { $filtered_image = $image; // Add 'width' and 'height' attributes if applicable. if ( $attachment_id > 0 && false === strpos( $filtered_image, ' width=' ) && false === strpos( $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 && false === strpos( $filtered_image, ' srcset=' ) ) { $filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id ); } // Add 'loading' attribute if applicable. if ( $add_img_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); } if ( $filtered_image !== $image ) { $content = str_replace( $image, $filtered_image, $content ); } } foreach ( $iframes as $iframe => $attachment_id ) { $filtered_iframe = $iframe; // Add 'loading' attribute if applicable. if ( $add_iframe_loading_attr && false === strpos( $filtered_iframe, ' loading=' ) ) { $filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context ); } if ( $filtered_iframe !== $iframe ) { $content = str_replace( $iframe, $filtered_iframe, $content ); } } return $content; }