check if Gutenberg is currently in use

Necessary API Functions/Methods:

You’ll need WP_Screen::is_block_editor() method to check if you are currently in the Gutenberg Editor (Since WordPress 5.0).

Also, if you install Gutenberg as a separate plugin, then you’ll have the is_gutenberg_page() function available to do the same check.

So for an overall solution, you’ll need to combine these two.

Of course, this has to be checked from the admin panel pages and when the internal data is ready to call the function. So you’ll have to do the check using a suitable hook. For example, if you check this using init hook, it will not work.

Gutenberg itself checks the is_gutenberg_page() function from gutenberg_init() function, which is loaded using replace_editor hook. So replace_editor hook is a good place to do this check.

However, I’d suggest the use of admin_enqueue_scripts for making the check, since:

  1. admin_enqueue_scripts is the first hook that is fired after the same is_gutenberg_page() check Gutenberg makes itself.

  2. Because of the nature of Gutenberg, you’re more likely to load external scripts / styles for your purpose.

  3. admin_enqueue_scripts is a well known hook and it’s only fired from admin panel pages. So front end is not affected by it.

Having said that, if all you need is to enqueue some scripts/styles, then you may use enqueue_block_editor_assets hook for editor assets and enqueue_block_assets hook for both the editor and frontend assets (since WordPress 5.0).

I’m not providing the related CODE here as it wasn’t directly asked in the question, but it may be enough for most purposes.

Solution:

Sample CODE for Gutenberg check

add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_action' );
function wpse_is_gutenberg_editor() {
    if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { 
        return true;
    }   
    
    $current_screen = get_current_screen();
    if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
        return true;
    }
    return false;
}
function wpse_gutenberg_editor_action() {
    if( wpse_is_gutenberg_editor() ) { 
        // your gutenberg editor related CODE here
    }   
    else {
        // this is not gutenberg.
        // this may not even be an editor, you need to check the screen if you need to check for another editor.
    }
}

Leave a Comment