should_load_block_assets_on_demand
云策文档标注
概述
should_load_block_assets_on_demand 是一个 WordPress 过滤器,用于控制块样式是否按需加载。开发者可以通过此过滤器优化前端资源加载,减少不必要的 CSS 文件请求。
关键要点
- 过滤器名称:should_load_block_assets_on_demand
- 参数:$load_assets_on_demand(布尔值),决定是否仅在块渲染时加载其资源
- 默认值:取决于 wp_should_load_separate_core_block_assets() 的结果,影响核心块样式表的加载方式
- 返回 false 时加载所有块资源,无论是否渲染;返回 true 时仅加载渲染的块资源
- 引入版本:6.8.0
代码示例
return apply_filters( 'should_load_block_assets_on_demand', $load_assets_on_demand );注意事项
- 相关函数:wp_should_load_block_assets_on_demand() 用于检查块样式是否应仅按需加载
- 此过滤器影响前端性能,需根据站点具体需求谨慎配置
原文内容
Filters whether block styles should be loaded on demand.
Description
Returning false loads all block assets, regardless of whether they are rendered in a page or not.
Returning true loads block assets only when they are rendered.
The default value of the filter depends on the result of wp_should_load_separate_core_block_assets(), which controls whether Core block stylesheets should be loaded separately or via a combined ‘wp-block-library’ stylesheet.
Parameters
$load_assets_on_demandbool-
Whether to load block assets only when they are rendered.
Source
return apply_filters( 'should_load_block_assets_on_demand', $load_assets_on_demand );
Changelog
| Version | Description |
|---|---|
| 6.8.0 | Introduced. |