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 at 17:18

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.
    }   
}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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