admin管理员组

文章数量:1134232

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.

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.

Share Improve this question edited Jul 29, 2018 at 17:08 mmm 3,8073 gold badges16 silver badges22 bronze badges asked Jul 29, 2018 at 16:48 user8292439user8292439 4191 gold badge3 silver badges16 bronze badges 1
  • Do you mean like is_gutenberg_page() with function_exists (untested)? – birgire Commented Jul 29, 2018 at 17:18
Add a comment  | 

9 Answers 9

Reset to default 15

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

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

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

The WP_Screen Object now contains a boolean property for this. The following is working for me:

if( ! function_exists( 'get_current_screen' ) ) {
  return false;
}

$screen = get_current_screen();
$is_block_editor = $screen->is_block_editor;

See https://developer.wordpress.org/reference/classes/wp_screen/is_block_editor/

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 )

Here I have shared the custom function which you can use to check Gutenberg is enable or not.

if ( is_gutenberg_enable() ) {
    // Do your code
}

This is a custom function:

/**
 * Check if Gutenberg is enable.
 * Must be used not earlier than plugins_loaded action fired.
 *
 * @return bool
 */
function is_gutenberg_enable() {

    $gutenberg    = false;
    $block_editor = false;

    if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) {
        // Gutenberg is installed and activated.
        $gutenberg = true;
    }

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

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

    include_once ABSPATH . 'wp-admin/includes/plugin.php';

    if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
        return true;
    }

    $use_block_editor = ( get_option( 'classic-editor-replace' ) === 'no-replace' );

    return $use_block_editor;
}

In WP 5.0 and higher, a function 'has_blocks' is present in /wp-includes/blocks.php, so you can use:

    if ( function_exists('has_blocks')) {
        $this->isGutenberg = true;
    }
    else {
        $this->isGutenberg = false;
    }

Check if Gutenberg is active.

Not Call Class if ( zwp_is_block_editor() ) {

Error 
       if ( zwp_is_block_editor() ) {
             class Auto_Save_Images_BLock_Editor
             {
                 ..............
             }
             new Auto_Save_Images_BLock_Editor();
       }

 OK
       if ( ! zwp_is_block_editor() ) {
             class Auto_Save_Images_Classic_Editor
             {
                 ..............
             }
             new Auto_Save_Images_Classic_Editor();
       }



if ( ! function_exists( 'zwp_is_block_editor' ) ) {

    function zwp_is_block_editor() {
        / 
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        $is_block_editor = $screen ? $screen->is_block_editor() : false;

        if ( $is_block_editor ) {
            // Do your block editor stuff here
            return true;
        } else {
            // Do your classic editor stuff here
            return false;
        }

    }
}

本文标签: plugin developmentcheck if Gutenberg is currently in use