gutenbergdocs/how-to-guides/curating-the-editor-experience/disable-editor-functionality.md
2025-10-22 01:33:45 +08:00

125 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 禁用编辑器功能
本文专门介绍在文章编辑器和站点编辑器中禁用特定功能的多种方法,这些内容未包含在管理文档的其他部分。
## 限制区块选项
有时您可能希望完全禁止用户使用某些区块。要控制插入器中可用的内容,您可以采用两种方法:[允许列表](/docs/reference-guides/filters/block-filters.md#using-an-allow-list)(仅启用列表中的区块)或[拒绝列表](/docs/reference-guides/filters/block-filters.md#using-a-deny-list)(取消注册特定区块)。
## 管理标题层级
具有标题层级下拉菜单的WordPress核心区块支持`levelOptions`属性。这适用于标题、站点标题、站点标语、查询标题、文章标题和评论标题区块。`levelOptions`属性接受对应标题层级的数字数组,其中`1`代表H1`2`代表H2依此类推。
此属性允许您指定哪些标题层级应出现在下拉UI中提供了一种轻量级的管理方法无需弃用区块。现有标题层级会在标记中保留而`levelOptions`仅影响UI显示。
您可以直接在区块标记中应用此属性,这种技术将常用于区块模板、模板部件和模式。例如,以下标记通过设置`"levelOptions":[3,4,5]`在标题区块中禁用H1、H2和H6。
```html
<!-- wp:heading {"level":3,"levelOptions":[3,4,5],"className":"wp-block-heading"} -->
<h3 class="wp-block-heading">标记示例</h3>
<!-- /wp:heading -->
```
您也可以使用[区块过滤器](/docs/reference-guides/filters/block-filters.md)全局或为特定区块设置此属性的默认值。以下示例为所有标题区块禁用H1、H2和H6。您可以通过基于用户权限等条件限制特定标题层级来进一步自定义。
```php
function example_modify_heading_levels_globally( $args, $block_type ) {
if ( 'core/heading' !== $block_type ) {
return $args;
}
// 移除H1、H2和H6
$args['attributes']['levelOptions']['default'] = [ 3, 4, 5 ];
return $args;
}
add_filter( 'register_block_type_args', 'example_modify_heading_levels_globally', 10, 2 );
```
## 禁用模式目录
要从插入器中完全移除WordPress核心捆绑的模式可将以下代码添加到您的`functions.php`文件:
```php
function example_theme_support() {
remove_theme_support( 'core-block-patterns' );
}
add_action( 'after_setup_theme', 'example_theme_support' );
```
## 禁用区块变体
某些核心区块实际上是[区块变体](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-variations/)。一个很好的例子是行和堆栈区块,它们实际上是群组区块的变体。如果您想禁用这些"区块",实际上需要禁用相应的变体。
区块变体使用JavaScript注册需要使用JavaScript禁用。以下代码将禁用行变体。
```js
wp.domReady( () => {
wp.blocks.unregisterBlockVariation( 'core/group', 'group-row' );
});
```
假设代码放置在主题文件夹根目录的`disable-variations.js`文件中,您可以使用以下代码在主题的`functions.php`中加载此文件。
```php
function example_disable_variations_script() {
wp_enqueue_script(
'example-disable-variations-script',
get_template_directory_uri() . '/disable-variations.js',
array( 'wp-dom-ready' ),
wp_get_theme()->get( 'Version' ),
true
);
}
add_action( 'enqueue_block_editor_assets', 'example_disable_variations_script' );
```
## 禁用区块样式
有几个核心区块包含自己的[区块样式](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/)。例如图像区块包含一个名为"圆形"的圆角图像样式。您可能不希望用户使用圆形图像,或者更倾向于使用边框半径控件而非区块样式。无论哪种情况,都可以轻松禁用不需要的区块样式。
与区块变体不同您可以使用JavaScript或PHP注册样式。如果样式是用JavaScript注册的必须用JavaScript禁用。如果使用PHP注册可以用任一方法禁用样式。所有核心区块样式都是用JavaScript注册的。
因此,您可以使用以下代码禁用图像区块的"圆形"样式。
```js
wp.domReady( () => {
wp.blocks.unregisterBlockStyle( 'core/image', 'rounded' );
});
```
此JavaScript的加载方式与上面的区块变体示例类似。有关使用PHP注册和取消注册样式的方法请参阅[区块样式](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/)文档。
## 禁用代码编辑器访问权限
代码编辑器允许您查看页面或文章的底层区块标记。虽然此视图对有经验的用户很方便,但编辑内容可能会意外破坏区块标记。将以下代码添加到`functions.php`文件以限制访问。
```php
function example_restrict_code_editor_access( $settings, $context ) {
$settings[ 'codeEditingEnabled' ] = false;
return $settings;
}
add_filter( 'block_editor_settings_all', 'example_restrict_code_editor_access', 10, 2 );
```
此代码阻止所有用户访问代码编辑器。您也可以添加[权限](https://wordpress.org/documentation/article/roles-and-capabilities/)检查来为特定用户禁用访问。
## 禁用富文本区块的格式化选项
支持[富文本](https://developer.wordpress.org/block-editor/reference-guides/richtext/)的区块附带WordPress提供的默认格式化选项。
需要使用`unregisterFormatType`通过JavaScript禁用格式化选项。以下代码将全局禁用内联图像、语言、键盘输入、下标和上标选项。
```js
wp.domReady( () => {
wp.richText.unregisterFormatType( 'core/image' );
wp.richText.unregisterFormatType( 'core/language' );
wp.richText.unregisterFormatType( 'core/keyboard' );
wp.richText.unregisterFormatType( 'core/subscript' );
wp.richText.unregisterFormatType( 'core/superscript' );
});
```
此JavaScript的加载方式与上面的区块变体示例类似。