块编辑器开发文档

恢复经典小部件编辑器

💡 云策文档标注

概述

本文档介绍了禁用新的小部件块编辑器的多种方法,包括通过主题、插件和过滤器,以恢复经典小部件编辑器。

关键要点

  • 主题可以通过调用 remove_theme_support('widgets-block-editor') 来禁用小部件块编辑器。
  • 最终用户可以通过安装并激活 Classic Widgets 插件来切换小部件块编辑器的启用状态。
  • 使用 use_widgets_block_editor 过滤器可以控制小部件块编辑器是否启用,支持全局禁用或针对特定用户的自定义逻辑。

代码示例

// 通过主题禁用小部件块编辑器
function example_theme_support() {
    remove_theme_support( 'widgets-block-editor' );
}
add_action( 'after_setup_theme', 'example_theme_support' );

// 通过过滤器全局禁用小部件块编辑器
add_filter( 'use_widgets_block_editor', '__return_false' );

// 通过过滤器为特定用户禁用小部件块编辑器
function example_use_widgets_block_editor( $use_widgets_block_editor ) {
    if ( 123 === get_current_user_id() ) {
        return false;
    }
    return $use_widgets_block_editor;
}
add_filter( 'use_widgets_block_editor', 'example_use_widgets_block_editor' );

📄 原文内容

There are several ways to disable the new Widgets Block Editor.

Using remove_theme_support

Themes may disable the Widgets Block Editor by calling remove_theme_support( 'widgets-block-editor' ).

For example, a theme may have the following PHP code in functions.php.

function example_theme_support() {
    remove_theme_support( 'widgets-block-editor' );
}
add_action( 'after_setup_theme', 'example_theme_support' );

Using the Classic Widgets plugin

End users may disable the Widgets Block Editor by installing and activating the Classic Widgets plugin.

With this plugin installed, the Widgets Block Editor can be toggled on and off by deactivating and activating the plugin.

Using a filter

the use_widgets_block_editor filter controls whether or not the Widgets Block Editor is enabled.

For example, a site administrator may include the following PHP code in a mu-plugin to disable the Widgets Block Editor.

add_filter( 'use_widgets_block_editor', '__return_false' );

For more advanced uses, you may supply your own function. In this example, the Widgets Block Editor is disabled for a specific user.

function example_use_widgets_block_editor( $use_widgets_block_editor ) {
    if ( 123 === get_current_user_id() ) {
        return false;
    }
    return $use_widgets_block_editor;
}
add_filter( 'use_widgets_block_editor', 'example_use_widgets_block_editor' );