apply_filters( 'wp_img_tag_add_loading_attr', string|bool $value, string $image, string $context )

Filters the loading attribute value to add to an image. Default lazy.


Returning false or an empty string will not add the attribute.
Returning true will add the default value.

Top ↑


$value string|bool
The loading attribute value. Returning a falsey value will result in the attribute being omitted for the image.
$image string
The HTML img tag to be filtered.
$context string
Additional context about how the function was called or where the img tag is.

Top ↑


File: wp-includes/media.php. View all references

$value = apply_filters( 'wp_img_tag_add_loading_attr', $value, $image, $context );

Top ↑


Version Description
5.5.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Kresimir Pendic

    tl;dr тик this will fix your LCP in pagespeed report for lazy loaded images above the fold

    This filter is very useful for images above the fold that don’t need to be lazy loaded (for example logos, featured images etc) so we can hook this filter to read our images and parse for example some custom class that can set lazy loading attribute ON or OFF per image.

    – by setting custom class `skip-lazy` inside admin under `Advanced > Additional Classes` inside image settings:

    add_filter( 'wp_img_tag_add_loading_attr', function( $value, $image ){
    	if ( false !== strpos( $image, 'skip-lazy' ) ){
    		return false;
    	return true;
    }, 10, 2 );

    disables it

You must log in before being able to contribute a note or feedback.