12

How can I check if the editor that is currently being used is Gutenberg in a WordPress plugin?

I need this because Gutenberg lacks post_submitbox_misc_actions, so I need a fallback which will only be used if the current editor is Gutenberg.

  • Do you mean like is_gutenberg_page() with function_exists (untested)? – birgire Jul 29 '18 at 17:18
9

There is is_gutenberg_page() function that'll be present when you activate Gutenberg, so you may check:

if( function_exists( 'is_gutenberg_page' ) )

This will only check if Gutenberg is activated, and the function itself will check if the current editor is set to load Gutenberg. So the CODE becomes:

if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() )

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.

Sample CODE (tested):

add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_test' );
function wpse_gutenberg_editor_test() {
    if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { 
        // your gutenberg editor related CODE here
    }   
    else {
        // this is not gutenberg.
        // this may not even be any editor, you need to check the screen.
    }   
}
  • 2
    This is only working with the Gutenberg plugin, it doesn't work in wp5 if plugin is not installed – maxime schoeni Nov 17 '18 at 14:01
  • 1
    is_block_editor is required for wp5.0 + – Jacob Raccuia Jan 15 at 6:07
5

The function is_gutenberg_page is from the Gutenberg plugin, while the is_block_editor method is available from 5.0. This function below combines both into a single checking function.

The code below is from Freemius SDK, props to their team:

function is_gutenberg_page() {
    if ( function_exists( 'is_gutenberg_page' ) &&
            is_gutenberg_page()
    ) {
        // The Gutenberg plugin is on.
        return true;
    }
    $current_screen = get_current_screen();
    if ( method_exists( $current_screen, 'is_block_editor' ) &&
            $current_screen->is_block_editor()
    ) {
        // Gutenberg page on 5+.
        return true;
    }
    return false;
}
  • This is the correct answer moving forward. – Jacob Raccuia Jan 15 at 6:07
3
  1. Gutenberg has been integrated in WordPress 5.0 and now you can check by using use_block_editor_for_post function.

    if(use_block_editor_for_post($post)){
      //Block editor is active for this post.
    }
    
  2. Alternatively, when creating new post you can use use_block_editor_for_post_type function to check if gutenberg is active for this post type.

    if(use_block_editor_for_post_type($postType)){
    //Gutenberg is active.
    }
    
2

has_blocks is the way to go to check content, but also note if you are just checking if the block editor screen is being used in the admin area, you can do a check like this (to account for both the new block editor and Gutenberg plugin):

if (is_admin()) {
    global $current_screen;
    if (!isset($current_screen)) {$current_screen = get_current_screen();}
    if ( ( method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor() )
      || ( function_exists('is_gutenberg_page')) && is_gutenberg_page() ) ) {
        // DO SOMETHING HERE
    }
}
  • This is great, but I don't think you need to call global $current_screen. – Simon Cossar Dec 9 '18 at 23:55
  • true, I tend to code examples this way as I don't know what context they will be in - they could break easily depending on that... I just added an additional is_admin and isset check for this purpose. – majick Dec 10 '18 at 3:49
  • In the current WP version (5.0.2) the above snippet does not work when it is executed in a function attached to the current_screen hook because is_block_editor is set later than that. It only works if it is executed at a later time (i.e. load-(page)). This seems a bug in WP. – marcochiesi Jan 8 at 18:13
1

Gutenberg 3.6 introduced functions like has_blocks and has_block. They replace the deprecated gutenberg_post_has_blocks function.

If has_blocks returns true than Gutenberg was used while editing the post.

You can either use has_blocks() without params if the $post global is already set(for something like a query loop) or check the post content directly with has_blocks( $content )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.