wp_lazy_loading_enabled( string $tag_name, string $context )

Determine whether to add the loading attribute to the specified tag in the specified context.


Parameters Parameters

$tag_name

(string) (Required) The tag name.

$context

(string) (Required) Additional context, like the current filter name or the function name from where this was called.


Top ↑

Return Return

(bool) Whether to add the attribute.


Top ↑

Source Source

File: wp-includes/media.php

function wp_lazy_loading_enabled( $tag_name, $context ) {
	// By default add to all 'img' tags.
	// See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading
	$default = ( 'img' === $tag_name );

	/**
	 * Filters whether to add the `loading` attribute to the specified tag in the specified context.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $default  Default value.
	 * @param string $tag_name The tag name.
	 * @param string $context  Additional context, like the current filter name or the function name from where this was called.
	 */
	return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by shaunjeffrey

    wp_lazy_loading_enabled returns true for all tags, which is every time this function is called by default. Hooking into the wp_lazy_loading_enabled filter 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.

    add_filter( 'wp_lazy_loading_enabled', '__return_false' );
    

    1. To change how this feature works for specific attachments, the ‘loading’ attribute can be supplied to the wp_get_attachment_image function, or you can modify or remove the attribute using the wp_get_attachment_image_attributes filter.

    $attr['loading'] = false;
    return wp_get_attachment_image( $attachment_id, $size, $icon, $attr );
    

    Or

    add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment, $size ) {
    	if ( $attachment->post_mime_type === 'image/svg+xml' ) {
    		unset( $attr['loading'] );
    	}
    	return $attr;
    } );
    

    2. To change how this works for specific avatars, you can either redefine the get_avatar function, supply the ‘loading’ attribute within the ‘extra_attr’ argument, or replace the HTML using the get_avatar filter.

    3. the_content, the_excerpt and widget_text_filters will call the wp_filter_content_tags filter on the content, which will also add the ‘loading’ attribute to image tags. To adjust this, the wp_img_tag_add_loading_attr filter can be used to manage the ‘loading’ attribute on a tag by tag basis.

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