The new editor called Gutenberg is here as plugin in 4.9, and as core functionality called Block Editor, in 5.0. Regarding to it, it is often needed to determine programmatically which editor is used to edit post or page in the site console. How to do it?

  • 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. } } – vikrant zilpe Nov 30 at 11:06
  • and this davidsword.ca/development/… – vikrant zilpe Nov 30 at 11:09
  • 2
    @vikrantzilpe please do not rely to an outdated information. It is better to test what you are writing. For example, the_gutenberg_project() function exists only in Gutenberg plugin, but not in the WP 5.0 Core. – KAGG Design Nov 30 at 11:23
  • ok thanks inform me – vikrant zilpe Nov 30 at 11:25

There are several variants:

  • WordPress 4.9, Gutenberg plugin is not active
  • WordPress 4.9, Gutenberg plugin is active
  • WordPress 5.0, Block Editor by default
  • WordPress 5.0, Classic Editor plugin is active
  • WordPress 5.0, Classic Editor plugin is active, but in site console in “Settings > Writing” the option “Use the Block editor by default…” is selected

All the mentioned variants can be processed by the following code:

/**
 * Check if Block Editor is active.
 * Must only be used after plugins_loaded action is fired.
 *
 * @return bool
 */
function is_active() {
    // Gutenberg plugin is installed and activated.
    $gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );

    // Block editor since 5.0.
    $block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );

    if ( ! $gutenberg && ! $block_editor ) {
        return false;
    }

    if ( self::is_classic_editor_plugin_active() ) {
        $editor_option       = get_option( 'classic-editor-replace' );
        $block_editor_active = array( 'no-replace', 'block' );

        return in_array( $editor_option, $block_editor_active, true );
    }

    return true;
}

Function returns true if block editor is active by any means, and false – in the case if classic editor is here. This function must only be used after plugins_loaded action is fired.

P.S. Due release of version 1.2 of Classic Editor plugin, code is updated, as classic-editor-replace options now takes values not replace and no-replace, but classic and block.

  • not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them. – Mark Kaplun Nov 30 at 16:02
  • or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks – Mark Kaplun Nov 30 at 16:05

You can use

add_action( 'enqueue_block_editor_assets', 'your_function_name' );

which is only fired when editing content with Gutenberg.

  • This code works only on editor page. – KAGG Design Dec 3 at 4:09
  • What about enqueue_block_assets? – Marc Dec 3 at 16:43
  • has_action( 'enqueue_block_assets' ) does the same what has_filter( 'replace_editor' in my code. It returns true if Classic Editor active and sets Classic Editor as default. It is better to check what you propose before writing. – KAGG Design 2 days ago

Your Answer

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.