有几种变体:
WordPress 4.9,Gutenberg插件处于非活动状态,Gutenberg插件处于活动状态,WordPress 5.0,默认情况下块编辑器处于活动状态,经典编辑器插件处于活动状态,但在site console的“Settings”>“Writing”中,选择了“Use the Block editor by default…”选项,所有提到的变体都可以通过以下代码处理:
/**
* 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;
}
如果块编辑器以任何方式处于活动状态,则函数返回true;如果存在classic editor,则函数返回false。此功能只能在
plugins_loaded
操作已启动。
P、 经典编辑器插件1.2版即将在美国发布,代码更新如下classic-editor-replace
选项现在接受值而不是replace
和no-replace
, 但是classic
和block
.