Determines whether to add the loading attribute to the specified tag in the specified context.
Parameters
$tag_namestringrequired- The tag name.
$contextstringrequired- Additional context, like the current filter name or the function name from where this was called.
Source
// Check if there is already a 'sizes' attribute.
$sizes = strpos( $image, ' sizes=' );
if ( ! $sizes ) {
$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
}
}
if ( $srcset && $sizes ) {
// Format the 'srcset' and 'sizes' string and escape attributes.
$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );
if ( is_string( $sizes ) ) {
$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
}
// Add the srcset and sizes attributes to the image markup.
return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
}
wp_lazy_loading_enabledreturns true for alltags, which is every time this function is called by default. Hooking into thewp_lazy_loading_enabledfilter is the primary way of enabling or disabling this feature, however it does not readily allow this to be enabled or disabled for specific attachments or mime-types.1. To change how this feature works for specific attachments, the ‘loading’ attribute can be supplied to the
wp_get_attachment_imagefunction, or you can modify or remove the attribute using thewp_get_attachment_image_attributesfilter.Or
2. To change how this works for specific avatars, you can either redefine the
get_avatarfunction, supply the ‘loading’ attribute within the ‘extra_attr’ argument, or replace the HTML using theget_avatarfilter.3.
the_content,the_excerptandwidget_text_filterswill call thewp_filter_content_tagsfilter on the content, which will also add the ‘loading’ attribute to image tags. To adjust this, thewp_img_tag_add_loading_attrfilter can be used to manage the ‘loading’ attribute on a tag by tag basis.