wp_lazy_loading_enabled
Allows you to enable or disable the loading attribute for the specified tag in the specified context.
Usage
add_filter( 'wp_lazy_loading_enabled', 'wp_kama_lazy_loading_enabled_filter', 10, 3 );
/**
* Function for `wp_lazy_loading_enabled` filter-hook.
*
* @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
*/
function wp_kama_lazy_loading_enabled_filter( $default, $tag_name, $context ){
// filter...
return $default;
}
- $default(true/false)
- The default value.
- $tag_name(string)
- The tag for which the
loadingattribute should be output. - $context(string)
- The context in which this hook fires. This is the name of the function in which the filter is called, for example,
get_avatar.
Examples
#1 Disable the loading="lazy" attribute in get_avatar()
By default, get_avatar() adds the loading="lazy" attribute to the returned IMG tag. Suppose this attribute is unwanted and must be removed. Add the following code to the theme's functions.php file or elsewhere:
add_filter( 'wp_lazy_loading_enabled', 'disable_get_avatar_lazy_loading', 10, 3 );
function disable_get_avatar_lazy_loading( $default, $tag_name, $context ){
if( 'img' === $tag_name && 'get_avatar' === $context )
return false;
return $default;
}Changelog
| Since 5.5.0 | Introduced. |
Where the hook is called
wp_lazy_loading_enabled
wp-includes/media.php 1962
return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );