函数文档

wp_should_load_block_editor_scripts_and_styles()

💡 云策文档标注

概述

wp_should_load_block_editor_scripts_and_styles() 函数用于检查当前屏幕是否需要为所有已注册的区块类型加载编辑器脚本和样式。它返回一个布尔值,指示是否应将这些资源入队。

关键要点

  • 函数检查当前屏幕是否为区块编辑器屏幕,基于 WP_Screen 实例的 is_block_editor() 方法。
  • 返回结果可通过 should_load_block_editor_scripts_and_styles 过滤器进行自定义修改。
  • 该函数自 WordPress 5.6.0 版本引入,常用于控制区块编辑器资源的加载。

代码示例

function wp_should_load_block_editor_scripts_and_styles() {
	global $current_screen;

	$is_block_editor_screen = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor();

	/**
	 * Filters the flag that decides whether or not block editor scripts and styles
	 * are going to be enqueued on the current screen.
	 *
	 * @since 5.6.0
	 *
	 * @param bool $is_block_editor_screen Current value of the flag.
	 */
	return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
}

注意事项

  • 函数依赖于全局变量 $current_screen,确保在适当的环境下调用。
  • 开发者可以利用 should_load_block_editor_scripts_and_styles 过滤器来覆盖默认行为,例如在特定条件下强制加载或阻止资源。
  • 相关函数如 wp_common_block_scripts_and_styles() 和 wp_enqueue_registered_block_scripts_and_styles() 可能基于此函数的返回值来执行资源入队操作。

📄 原文内容

Checks if the editor scripts and styles for all registered block types should be enqueued on the current screen.

Return

bool Whether scripts and styles should be enqueued.

Source

function wp_should_load_block_editor_scripts_and_styles() {
	global $current_screen;

	$is_block_editor_screen = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor();

	/**
	 * Filters the flag that decides whether or not block editor scripts and styles
	 * are going to be enqueued on the current screen.
	 *
	 * @since 5.6.0
	 *
	 * @param bool $is_block_editor_screen Current value of the flag.
	 */
	return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
}

Hooks

apply_filters( ‘should_load_block_editor_scripts_and_styles’, bool $is_block_editor_screen )

Filters the flag that decides whether or not block editor scripts and styles are going to be enqueued on the current screen.

Changelog

Version Description
5.6.0 Introduced.