Using WordPress ‘enqueue_block_editor_assets’ PHP action

The enqueue_block_editor_assets WordPress PHP action allows you to enqueue custom scripts and styles for the block editor.

Usage

add_action('enqueue_block_editor_assets', 'your_function_name');
function your_function_name() {
    // your custom code here
    wp_enqueue_script('your-script-handle', 'your-script-source', array('dependencies'), 'your-version', true);
    wp_enqueue_style('your-style-handle', 'your-style-source', array('dependencies'), 'your-version');
}

Parameters

  • your_function_name (string): The name of your custom function that enqueues your scripts and styles.
  • your-script-handle (string): A unique handle for your script.
  • your-script-source (string): The URL for your script.
  • your-style-handle (string): A unique handle for your style.
  • your-style-source (string): The URL for your style.

More information

See WordPress Developer Resources: enqueue_block_editor_assets

Examples

Add custom JavaScript to the block editor

Enqueue a custom JavaScript file for the block editor.

add_action('enqueue_block_editor_assets', 'my_custom_block_editor_script');
function my_custom_block_editor_script() {
    wp_enqueue_script('my-script', 'path/to/your-script.js', array('wp-blocks'), '1.0.0', true);
}

Add custom CSS to the block editor

Enqueue a custom CSS file for the block editor.

add_action('enqueue_block_editor_assets', 'my_custom_block_editor_style');
function my_custom_block_editor_style() {
    wp_enqueue_style('my-style', 'path/to/your-style.css', array(), '1.0.0');
}

Enqueue both custom JavaScript and CSS for the block editor

Enqueue both a custom JavaScript file and a custom CSS file for the block editor.

add_action('enqueue_block_editor_assets', 'my_custom_block_editor_assets');
function my_custom_block_editor_assets() {
    wp_enqueue_script('my-script', 'path/to/your-script.js', array('wp-blocks'), '1.0.0', true);
    wp_enqueue_style('my-style', 'path/to/your-style.css', array(), '1.0.0');
}

Add custom font to the block editor

Enqueue a custom Google Font for the block editor.

add_action('enqueue_block_editor_assets', 'my_custom_block_editor_font');
function my_custom_block_editor_font() {
    wp_enqueue_style('google-fonts', 'https://fonts.googleapis.com/css?family=Roboto', array(), null);
}

Enqueue a script with localized data

Enqueue a custom script with localized data for the block editor.

add_action('enqueue_block_editor_assets', 'my_custom_block_editor_localized_script');
function my_custom_block_editor_localized_script() {
    wp_enqueue_script('my-script', 'path/to/your-script.js', array('wp-blocks'), '1.0.0', true);
    wp_localize_script('my-script', 'myScriptData', array(
        'key' => 'value',
    ));
}