wp_enqueue_registered_block_scripts_and_styles()
Enqueues registered block scripts and styles, depending on current rendered context (only enqueuing editor scripts while in context of the editor).
No Hooks.
Returns
null. Nothing (null).
Usage
wp_enqueue_registered_block_scripts_and_styles();
Changelog
| Since 5.0.0 | Introduced. |
wp_enqueue_registered_block_scripts_and_styles() wp enqueue registered block scripts and styles code WP 7.0
function wp_enqueue_registered_block_scripts_and_styles() {
if ( wp_should_load_block_assets_on_demand() ) {
/**
* Add placeholder for where block styles would historically get enqueued in a classic theme when block assets
* are not loaded on demand. This happens right after {@see wp_common_block_scripts_and_styles()} is called
* at which time wp-block-library is enqueued.
*/
if ( ! wp_is_block_theme() && has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) {
wp_register_style( 'wp-block-styles-placeholder', false );
wp_add_inline_style( 'wp-block-styles-placeholder', ':root { --wp-internal-comment: "Placeholder for wp_hoist_late_printed_styles() to replace with the block styles printed at wp_footer." }' );
wp_enqueue_style( 'wp-block-styles-placeholder' );
}
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 );
}
}
}
}