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
檢視更多 >