tpw_complete': // 重置密码 $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': // 我的首页评论 $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': // 单页 $pre .= $default_pre .= 'single_page.htm'; break; case 'search': // 搜索 $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': // 置顶 $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': // 关闭 $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': // 删除 $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': // 移动 $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: // 首页 $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } // 加载绑定ID安装风格 !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; // 加载安装风格 (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; // 主风格下可安装多个子风格 if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; // 加载绑定ID安装风格 $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; // 加载安装风格 !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } // 风格不存在加载适配端 !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } // 依据模式返回适配文件 function theme_mode_pre($type = 0) { global $config; // 网站模式 $mode = $config['setting']['website_mode']; $pre = ''; // 首页文件前缀 if (1 == $mode) { // 门户模式 $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { // 扁平模式 $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { // 自定义模式 $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>block editor - How to detect the usage of Gutenberg|Concepts Of Algorithm

block editor - How to detect the usage of Gutenberg

admin2025-06-04  0

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?

Update: There are number of outdated answers to similar question:

  • gutenberg_post_has_blocks() - this function exists only in Gutenberg plugin, and not in 5.0 Core
  • is_gutenberg_page() - the same
  • the_gutenberg_project() - the same
  • has_blocks() - does not work (returns false) when Classic Editor is on and its option "Default editor for all users" = "Block Editor"
  • answer simply produces fatal error Call to undefined function get_current_screen()

So, before commenting this question and answer, please take a work to check what do you propose. Check it now, with 4.9 and current version of WordPress, and all possible combinations of Classic Editor and Gutenberg/Block Editor. I will be happy to discuss tested solution, not links to something.

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?

Update: There are number of outdated answers to similar question:

  • gutenberg_post_has_blocks() - this function exists only in Gutenberg plugin, and not in 5.0 Core
  • is_gutenberg_page() - the same
  • the_gutenberg_project() - the same
  • has_blocks() - does not work (returns false) when Classic Editor is on and its option "Default editor for all users" = "Block Editor"
  • answer simply produces fatal error Call to undefined function get_current_screen()

So, before commenting this question and answer, please take a work to check what do you propose. Check it now, with 4.9 and current version of WordPress, and all possible combinations of Classic Editor and Gutenberg/Block Editor. I will be happy to discuss tested solution, not links to something.

Share Improve this question edited Jan 3, 2019 at 8:41 KAGG Design asked Nov 30, 2018 at 10:01 KAGG DesignKAGG Design 1,86210 silver badges15 bronze badges 8
  • 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 Commented Nov 30, 2018 at 11:06
  • please check this :artiss.blog/2018/09/detecting-per-post-use-of-gutenberg – vikrant zilpe Commented Nov 30, 2018 at 11:08
  • and this davidsword.ca/development/… – vikrant zilpe Commented Nov 30, 2018 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 Commented Nov 30, 2018 at 11:23
  • 3 Possible duplicate of check if Gutenberg is currently in use – T.Todua Commented Jan 2, 2019 at 9:05
 |  Show 3 more comments

2 Answers 2

Reset to default 13

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

/**
 * Check if Classic Editor plugin is active.
 *
 * @return bool
 */
function is_classic_editor_plugin_active() {
    if ( ! function_exists( 'is_plugin_active' ) ) {
        include_once ABSPATH . 'wp-admin/includes/plugin.php';
    }

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

    return false;
}

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.

You can use

add_action( 'enqueue_block_editor_assets', 'your_function_name' );

which is only fired when editing content with Gutenberg.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749049395a315923.html

lang[new_post](0)