wp_lazy_loading_enabled()
云策文档标注
概述
wp_lazy_loading_enabled() 函数用于判断是否在指定标签和上下文中添加 loading 属性,以控制懒加载行为。它默认对 'img' 和 'iframe' 标签启用,并通过过滤器提供自定义选项。
关键要点
- 函数返回布尔值,指示是否添加 loading 属性,参数包括标签名和上下文。
- 默认情况下,对 'img' 和 'iframe' 标签返回 true(从 WordPress 5.7.0 起 iframe 也默认启用)。
- 可通过 wp_lazy_loading_enabled 过滤器全局调整懒加载启用状态。
- 相关函数如 wp_get_loading_optimization_attributes() 和 wp_img_tag_add_loading_optimization_attrs() 依赖此函数。
代码示例
// 禁用所有懒加载
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
// 针对特定附件调整 loading 属性
$attr['loading'] = false;
return wp_get_attachment_image( $attachment_id, $size, $icon, $attr );
// 使用过滤器移除特定 MIME 类型的 loading 属性
add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment, $size ) {
if ( $attachment->post_mime_type === 'image/svg+xml' ) {
unset( $attr['loading'] );
}
return $attr;
} );注意事项
- 过滤器 wp_lazy_loading_enabled 是主要自定义方式,但可能不便于针对特定附件或 MIME 类型精细控制。
- 对于特定附件,可通过 wp_get_attachment_image 函数或 wp_get_attachment_image_attributes 过滤器管理 loading 属性。
- 内容过滤器如 the_content 会调用 wp_filter_content_tags,可使用 wp_img_tag_add_loading_attr 过滤器逐标签调整。
原文内容
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
function wp_lazy_loading_enabled( $tag_name, $context ) {
/*
* By default add to all 'img' and 'iframe' tags.
* See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading
* See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading
*/
$default = ( 'img' === $tag_name || 'iframe' === $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 );
}
Hooks
- apply_filters( ‘wp_lazy_loading_enabled’, bool $default, string $tag_name, string $context )
-
Filters whether to add the
loadingattribute to the specified tag in the specified context.
Skip to note 2 content
shaunjeffrey
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.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_imagefunction, or you can modify or remove the attribute using thewp_get_attachment_image_attributesfilter.$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_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.