wp_enqueue_registered_block_scripts_and_styles

函数
wp_enqueue_registered_block_scripts_and_styles ( No parameters )

wp_enqueue_registered_block_scripts_and_styles: 这个函数用于在WordPress中排队等候注册块所需的脚本和样式。块是新的WordPress编辑器的构建块,它被称为块编辑器: 当一个新的块被注册时,它可能需要特定的脚本和样式被排队,以便正常运行: 这个函数提供了一种方法来排队这些资产。

根据当前的渲染环境,排队注册区块脚本和样式(只有在编辑器的环境下才排队注册编辑脚本)。

function wp_enqueue_registered_block_scripts_and_styles() {
	global $current_screen;

	if ( wp_should_load_separate_core_block_assets() ) {
		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 );
			}
		}
	}
}

常见问题

FAQs
查看更多 >