Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use enqueue_block_editor_assets action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 26, 2023
5 minutes read

Let’s talk about the enqueue_block_editor_assets action in WordPress. As a fellow developer, especially if you’re working with WordPress in India, you’ll know how important it is to handle assets efficiently. This hook comes in handy when you want to add custom scripts or styles to the block editor.

To start using the enqueue_block_editor_assets action, you need to register it using add_action. You can place this code in your theme’s functions.php file or, better yet, in a custom WordPress plugin. At WePlugins, we always recommend creating a custom plugin to avoid issues during theme updates.

Below are some live examples showing how you can effectively use this hook.

Example 1: Enqueuing Scripts for Block Editor

In this example, we demonstrate how to enqueue a JavaScript file specifically for the block editor.

    function weplugins_enqueue_scripts() {
        $blockPath = '/blocks/index.js';
        // Enqueue the block index.js file
        wp_enqueue_script(
            'weplugins-example-block', // unique handle
            get_template_directory_uri() . $blockPath,
            [ 'wp-blocks', 'wp-element', 'wp-i18n' ], // required dependencies for blocks
            filemtime( get_template_directory() . $blockPath )
        );
    }
    add_action( 'enqueue_block_editor_assets', 'weplugins_enqueue_scripts' );
    

Example 2: Executing Custom Code on Action

Let’s see how to execute custom code when the enqueue_block_editor_assets action is triggered.

    function weplugins_execute_on_enqueue_block_editor_assets_event() {
        // Your custom code here
    }
    // add the action
    add_action( "enqueue_block_editor_assets", "weplugins_execute_on_enqueue_block_editor_assets_event" );
    

Example 3: Removing a Hook Callback

Sometimes, you might need to remove a registered hook. Here’s how you can do that.

    remove_action( "enqueue_block_editor_assets", "weplugins_execute_on_enqueue_block_editor_assets_event" );
    

Ensure you provide the same callback function name, priority, and number of arguments when removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you need any customization or assistance with this hook, feel free to Contact Us. Our team at WePlugins is always ready to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.