钩子文档

wp_min_priority_img_pixels

💡 云策文档标注

概述

wp_min_priority_img_pixels 是一个 WordPress 过滤器,用于设置图像作为高优先级图像的最小平方像素阈值。开发者可以通过此过滤器调整阈值,以优化图像加载优先级。

关键要点

  • 过滤器名称:wp_min_priority_img_pixels,默认阈值为 50000 平方像素。
  • 参数:$threshold(整数),表示最小平方像素阈值,默认值为 50000。
  • 用途:影响 wp_get_loading_optimization_attributes() 和 wp_maybe_add_fetchpriority_high_attr() 函数,用于确定是否将 fetchpriority='high' 属性添加到图像加载属性中。
  • 引入版本:WordPress 6.3.0。

代码示例

/**
 * Filters to set the minimum square-pixels threshold for an image to be eligible as the high-priority image.
 *
 * @param int  $threshold Minimum threshold value.
 * @return int $threshold Modified threshold value.
 */
function set_min_priority_img_pixels( $threshold ){
    // Set minimum threshold of 200x200 pixels for small thumbnail images.
    $threshold = 40000;
    return $threshold;
}
add_filter( 'wp_min_priority_img_pixels', 'set_min_priority_img_pixels' );

注意事项

设置过低的阈值可能导致优先加载视口外的图像,从而增加累积布局偏移(CLS),并可能优先处理 IMG 标签图像而非 CSS 背景图像。


📄 原文内容

Filters the minimum square-pixels threshold for an image to be eligible as the high-priority image.

Parameters

$thresholdint
Minimum square-pixels threshold. Default 50000.

Source

$wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 );

Changelog

Version Description
6.3.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Filter minimum square-pixels threshold value for small thumbnail images that appear in lists or grids, a minimum of 200×200 pixels might be suitable.

    /**
     * Filters to set the minimum square-pixels threshold for an image to be eligible as the high-priority image.
     *
     * @param int  $threshold Minimum threshold value.
     * @return int $threshold Modified threshold value.
     */
    function set_min_priority_img_pixels( $threshold ){
        // Set minimum threshold of 200x200 pixels for small thumbnail images.
        $threshold = 40000;
        return $threshold;
    }
    add_filter( 'wp_min_priority_img_pixels', 'set_min_priority_img_pixels' );