wp_get_loading_attr_default()
概述
wp_get_loading_attr_default() 函数用于获取元素 loading 属性的默认值,通常返回 'lazy',但会根据上下文和启发式规则判断是否省略该属性以优化性能。该函数自 WordPress 6.3.0 起已弃用,建议使用 wp_get_loading_optimization_attributes() 替代。
关键要点
- 函数返回 'lazy'、'eager' 或布尔值 false,false 表示应省略 loading 属性。
- 通过 wp_increase_content_media_count() 计数内容媒体元素,默认前 3 个元素会省略 loading 属性,可通过 'wp_omit_loading_attr_threshold' 过滤器自定义阈值。
- 针对特定上下文(如 'template'、'template_part_header'、'the_content'、'the_post_thumbnail')有特殊处理逻辑,以避免对首屏元素进行懒加载。
- 函数已弃用,开发者应迁移至 wp_get_loading_optimization_attributes()。
代码示例
function wp_get_loading_attr_default( $context ) {
_deprecated_function( __FUNCTION__, '6.3.0', 'wp_get_loading_optimization_attributes()' );
global $wp_query;
// Skip lazy-loading for the overall block template, as it is handled more granularly.
if ( 'template' === $context ) {
return false;
}
/*
* Do not lazy-load images in the header block template part, as they are likely above the fold.
* For classic themes, this is handled in the condition below using the 'get_header' action.
*/
$header_area = WP_TEMPLATE_PART_AREA_HEADER;
if ( "template_part_{$header_area}" === $context ) {
return false;
}
// Special handling for programmatically created image tags.
if ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) {
/*
* Skip programmatically created images within post content as they need to be handled together with the other
* images within the post content.
* Without this clause, they would already be counted below which skews the number and can result in the first
* post content image being lazy-loaded only because there are images elsewhere in the post content.
*/
if ( doing_filter( 'the_content' ) ) {
return false;
}
// Conditionally skip lazy-loading on images before the loop.
if (
// Only apply for main query but before the loop.
$wp_query->before_loop && $wp_query->is_main_query()
/*
* Any image before the loop, but after the header has started should not be lazy-loaded,
* except when the footer has already started which can happen when the current template
* does not include any loop.
*/
&& did_action( 'get_header' ) && ! did_action( 'get_footer' )
) {
return false;
}
}
/*
* The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded,
* as they are likely above the fold.
*/
if ( 'the_content' === $context || 'the_post_thumbnail' === $context ) {
// Only elements within the main query loop have special handling.
if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
return 'lazy';
}
// Increase the counter since this is a main query content element.
$content_media_count = wp_increase_content_media_count();
// If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted.
if ( $content_media_count < wp_omit_loading_attr_threshold() ) {
return false;
}
}
return 'lazy';
}注意事项
- 函数仅在懒加载全局启用时调用,需确保上下文参数正确传递。
- 弃用状态意味着未来版本可能移除,建议及时更新代码以避免兼容性问题。
- 内部依赖多个 WordPress 核心函数(如 wp_increase_content_media_count()、in_the_loop() 等),调用时需注意环境条件。
Gets the default value to use for a loading attribute on an element.
Description
This function should only be called for a tag and context if lazy-loading is generally enabled.
The function usually returns ‘lazy’, but uses certain heuristics to guess whether the current element is likely to appear above the fold, in which case it returns a boolean false, which will lead to the loading attribute being omitted on the element. The purpose of this refinement is to avoid lazy-loading elements that are within the initial viewport, which can have a negative performance impact.
Under the hood, the function uses wp_increase_content_media_count() every time it is called for an element within the main content. If the element is the very first content element, the loading attribute will be omitted.
This default threshold of 3 content elements to omit the loading attribute for can be customized using the ‘wp_omit_loading_attr_threshold’ filter.
See also
Parameters
$contextstringrequired-
Context for the element for which the
loadingattribute value is requested.
Source
function wp_get_loading_attr_default( $context ) {
_deprecated_function( __FUNCTION__, '6.3.0', 'wp_get_loading_optimization_attributes()' );
global $wp_query;
// Skip lazy-loading for the overall block template, as it is handled more granularly.
if ( 'template' === $context ) {
return false;
}
/*
* Do not lazy-load images in the header block template part, as they are likely above the fold.
* For classic themes, this is handled in the condition below using the 'get_header' action.
*/
$header_area = WP_TEMPLATE_PART_AREA_HEADER;
if ( "template_part_{$header_area}" === $context ) {
return false;
}
// Special handling for programmatically created image tags.
if ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) {
/*
* Skip programmatically created images within post content as they need to be handled together with the other
* images within the post content.
* Without this clause, they would already be counted below which skews the number and can result in the first
* post content image being lazy-loaded only because there are images elsewhere in the post content.
*/
if ( doing_filter( 'the_content' ) ) {
return false;
}
// Conditionally skip lazy-loading on images before the loop.
if (
// Only apply for main query but before the loop.
$wp_query->before_loop && $wp_query->is_main_query()
/*
* Any image before the loop, but after the header has started should not be lazy-loaded,
* except when the footer has already started which can happen when the current template
* does not include any loop.
*/
&& did_action( 'get_header' ) && ! did_action( 'get_footer' )
) {
return false;
}
}
/*
* The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded,
* as they are likely above the fold.
*/
if ( 'the_content' === $context || 'the_post_thumbnail' === $context ) {
// Only elements within the main query loop have special handling.
if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
return 'lazy';
}
// Increase the counter since this is a main query content element.
$content_media_count = wp_increase_content_media_count();
// If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted.
if ( $content_media_count <= wp_omit_loading_attr_threshold() ) {
return false;
}
// For elements after the threshold, lazy-load them as usual.
return 'lazy';
}
// Lazy-load by default for any unknown context.
return 'lazy';
}
Changelog
| Version | Description |
|---|---|
| 6.3.0 | Deprecated. Use wp_get_loading_optimization_attributes() instead. |
| 5.9.0 | Introduced. |