函数文档

wp_common_block_scripts_and_styles()

💡 云策文档标注

概述

wp_common_block_scripts_and_styles() 函数用于处理块编辑器和前端共用的脚本与样式资源排队加载。它根据条件判断是否加载核心块资源,并触发相关钩子。

关键要点

  • 函数检查 is_admin() 和 wp_should_load_block_editor_scripts_and_styles() 条件,决定是否继续执行。
  • 默认加载 'wp-block-library' 样式,并根据主题支持和 wp_should_load_separate_core_block_assets() 条件加载 'wp-block-library-theme' 样式。
  • 触发 do_action('enqueue_block_assets') 钩子,允许开发者添加自定义块资源。

代码示例

function wp_common_block_scripts_and_styles() {
	if ( is_admin() && ! wp_should_load_block_editor_scripts_and_styles() ) {
		return;
	}

	wp_enqueue_style( 'wp-block-library' );

	if ( current_theme_supports( 'wp-block-styles' ) && ! wp_should_load_separate_core_block_assets() ) {
		wp_enqueue_style( 'wp-block-library-theme' );
	}

	do_action( 'enqueue_block_assets' );
}

注意事项

  • 使用 add_action 在 'wp_enqueue_scripts' 钩子之前添加动作到 'enqueue_block_assets',以自定义块资源。
  • 相关函数如 wp_should_load_separate_core_block_assets() 和 wp_should_load_block_editor_scripts_and_styles() 用于控制资源加载条件。

📄 原文内容

Handles the enqueueing of block scripts and styles that are common to both the editor and the front-end.

Source

function wp_common_block_scripts_and_styles() {
	if ( is_admin() && ! wp_should_load_block_editor_scripts_and_styles() ) {
		return;
	}

	wp_enqueue_style( 'wp-block-library' );

	if ( current_theme_supports( 'wp-block-styles' ) && ! wp_should_load_separate_core_block_assets() ) {
		wp_enqueue_style( 'wp-block-library-theme' );
	}

	/**
	 * Fires after enqueuing block assets for both editor and front-end.
	 *
	 * Call `add_action` on any hook before 'wp_enqueue_scripts'.
	 *
	 * In the function call you supply, simply use `wp_enqueue_script` and
	 * `wp_enqueue_style` to add your functionality to the Gutenberg editor.
	 *
	 * @since 5.0.0
	 */
	do_action( 'enqueue_block_assets' );
}

Hooks

do_action( ‘enqueue_block_assets’ )

Fires after enqueuing block assets for both editor and front-end.

Changelog

Version Description
5.0.0 Introduced.