enqueue_block_assets
Allows you to add Block Editor styles and scripts shared by the front end and back end, including inside the iframe.
It fires after block assets are enqueued by wp_common_block_scripts_and_styles(), which in turn is attached to these hooks:
add_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' ); add_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
Therefore, this hook must be used before admin_enqueue_scripts or wp_enqueue_scripts.
The callback normally uses wp_enqueue_script and wp_enqueue_style.
Since WordPress 6.3, assets added through this hook are also loaded in the iframed editor when all registered blocks use Block API version 3 or later and no classic meta boxes are used.
To enqueue assets only in the editor, use an is_admin() condition inside the callback.
Styles added through this hook apply both to block content and to the editor interface. If they are not scoped to specific selectors, they can affect editor UI elements. Use specific selectors such as .editor-styles-wrapper or .wp-block-* to prevent this.
Use enqueue_block_editor_assets to add scripts or styles only to admin pages containing the Block Editor. It does not add anything inside the iframe.
Usage
add_action( 'enqueue_block_assets', 'wp_kama_enqueue_block_assets_action' );
/**
* Function for `enqueue_block_assets` action-hook.
*
* @return void
*/
function wp_kama_enqueue_block_assets_action(){
// action...
}
Examples
#1 Add shared block display styles
The styles work while editing a post and on the front end.
add_action( 'enqueue_block_assets', 'example_general_style' );
function example_general_style(){
wp_enqueue_style( 'example', plugins_url( 'example.css', __FILE__ ) );
}
#2 Enqueue styles only in the admin area
To enqueue styles or scripts only in the admin area, use an is_admin() condition. For example:
/// Registers and enqueues CSS styles only for the admin area.
add_action( 'enqueue_block_assets', 'wpdocs_enqueue_editor_styles' );
function wpdocs_enqueue_editor_styles() {
if( is_admin() ){
wp_enqueue_style( 'prefix-style', THEME_URL . '/assets/styles/admin.css' );
}
}
Since WP 6.3, styles and scripts added through enqueue_block_assets are also enqueued for the iframed editor.
Changelog
| Since 5.0.0 | Introduced. |
Where the hook is called
do_action( 'enqueue_block_assets' );
do_action( 'enqueue_block_assets' );
Where the hook is used in WordPress
add_action( 'enqueue_block_assets', 'wp_enqueue_classic_theme_styles' );
add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
add_action( 'enqueue_block_assets', $callback );