wp_should_load_separate_core_block_assets()
概述
wp_should_load_separate_core_block_assets() 函数用于检查是否应为核心块加载单独的样式表。当返回 true 时,核心块使用各自的样式表;返回 false 时,所有核心块使用单一的 'wp-block-library' 合并样式表。此函数仅影响前端,不影响块编辑器界面。
关键要点
- 函数返回布尔值,决定核心块样式是否分开加载,默认返回 false(加载所有块资产)。
- 通过 should_load_separate_core_block_assets 过滤器可自定义返回值,控制样式加载行为。
- 在管理界面、REST API 端点或 feed 请求时,函数自动返回 false,确保样式合并加载。
- 与 wp_should_load_block_assets_on_demand() 相关,影响块资产按需加载的默认行为。
代码示例
function wp_should_load_separate_core_block_assets() {
if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
return false;
}
/**
* Filters whether block styles should be loaded separately.
*
* Returning false loads all core block assets, regardless of whether they are rendered
* in a page or not. Returning true loads core block assets only when they are rendered.
*
* @since 5.8.0
*
* @param bool $load_separate_assets Whether separate assets will be loaded.
* Default false (all block assets are loaded, even when not used).
*/
return apply_filters( 'should_load_separate_core_block_assets', false );
}注意事项
- 此函数仅适用于前端,块编辑器界面不受影响。
- 返回值默认通过 wp_should_load_block_assets_on_demand() 影响块资产按需加载,但该行为可单独调整。
- 相关函数包括 wp_should_load_block_assets_on_demand()、wp_enqueue_registered_block_scripts_and_styles() 和 register_block_style_handle()。
Checks whether separate styles should be loaded for core blocks.
Description
When this function returns true, other functions ensure that core blocks use their own separate stylesheets.
When this function returns false, all core blocks will use the single combined ‘wp-block-library’ stylesheet.
As a side effect, the return value will by default result in block assets to be loaded on demand, via the wp_should_load_block_assets_on_demand() function. This behavior can be separately altered via that function.
This only affects front end and not the block editor screens.
See also
Source
function wp_should_load_separate_core_block_assets() {
if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
return false;
}
/**
* Filters whether block styles should be loaded separately.
*
* Returning false loads all core block assets, regardless of whether they are rendered
* in a page or not. Returning true loads core block assets only when they are rendered.
*
* @since 5.8.0
*
* @param bool $load_separate_assets Whether separate assets will be loaded.
* Default false (all block assets are loaded, even when not used).
*/
return apply_filters( 'should_load_separate_core_block_assets', false );
}
Hooks
- apply_filters( ‘should_load_separate_core_block_assets’, bool $load_separate_assets )
-
Filters whether block styles should be loaded separately.
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |