wp_enqueue_registered_block_scripts_and_styles()
云策文档标注
概述
wp_enqueue_registered_block_scripts_and_styles() 函数用于根据当前渲染上下文(如编辑器或前端)自动入队已注册的区块脚本和样式。它通过检查条件(如是否按需加载资产)来决定入队哪些资源,确保高效加载。
关键要点
- 函数根据 wp_should_load_block_assets_on_demand() 判断是否按需加载区块资产,若为 true 则直接返回,避免不必要的入队。
- 使用 wp_should_load_block_editor_scripts_and_styles() 和 is_admin() 确定是否在编辑器上下文中,以决定是否入队编辑器专用的脚本和样式。
- 通过 WP_Block_Type_Registry::get_instance() 获取所有已注册区块类型,并遍历其 style_handles、script_handles、editor_style_handles 和 editor_script_handles 进行入队操作。
- 区块样式仅在注册时入队,核心区块可能使用单独的 'wp-block-library' 样式表,具体取决于 wp_should_load_separate_core_block_assets() 的返回值。
代码示例
function wp_enqueue_registered_block_scripts_and_styles() {
if ( wp_should_load_block_assets_on_demand() ) {
return;
}
$load_editor_scripts_and_styles = is_admin() && wp_should_load_block_editor_scripts_and_styles();
$block_registry = WP_Block_Type_Registry::get_instance();
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
// Front-end and editor styles.
foreach ( $block_type->style_handles as $style_handle ) {
wp_enqueue_style( $style_handle );
}
// Front-end and editor scripts.
foreach ( $block_type->script_handles as $script_handle ) {
wp_enqueue_script( $script_handle );
}
if ( $load_editor_scripts_and_styles ) {
// Editor styles.
foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
wp_enqueue_style( $editor_style_handle );
}
// Editor scripts.
foreach ( $block_type->editor_script_handles as $editor_script_handle ) {
wp_enqueue_script( $editor_script_handle );
}
}
}
}注意事项
- 函数在 WordPress 5.0.0 版本中引入,是区块编辑器功能的一部分。
- 相关函数包括 wp_should_load_block_assets_on_demand()、wp_should_load_block_editor_scripts_and_styles() 和 wp_enqueue_style()/wp_enqueue_script(),开发者需了解这些辅助函数的作用以正确使用。
- 区块样式入队时不会触发警告,即使样式未注册,因此可以无条件调用 wp_enqueue_style()。
原文内容
Enqueues registered block scripts and styles, depending on current rendered context (only enqueuing editor scripts while in context of the editor).
Source
function wp_enqueue_registered_block_scripts_and_styles() {
if ( wp_should_load_block_assets_on_demand() ) {
return;
}
$load_editor_scripts_and_styles = is_admin() && wp_should_load_block_editor_scripts_and_styles();
$block_registry = WP_Block_Type_Registry::get_instance();
/*
* Block styles are only enqueued if they're registered. For core blocks, this is only the case if
* `wp_should_load_separate_core_block_assets()` returns true. Otherwise they use the single combined
* 'wp-block-library` stylesheet. See also `register_core_block_style_handles()`.
* Since `wp_enqueue_style()` does not trigger warnings if the style is not registered, it is okay to not cater for
* this behavior here and simply call `wp_enqueue_style()` unconditionally.
*/
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
// Front-end and editor styles.
foreach ( $block_type->style_handles as $style_handle ) {
wp_enqueue_style( $style_handle );
}
// Front-end and editor scripts.
foreach ( $block_type->script_handles as $script_handle ) {
wp_enqueue_script( $script_handle );
}
if ( $load_editor_scripts_and_styles ) {
// Editor styles.
foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
wp_enqueue_style( $editor_style_handle );
}
// Editor scripts.
foreach ( $block_type->editor_script_handles as $editor_script_handle ) {
wp_enqueue_script( $editor_script_handle );
}
}
}
}
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |