use_block_editor_for_post
云策文档标注
概述
use_block_editor_for_post 是一个 WordPress 过滤器,用于控制文章是否能在块编辑器中编辑。开发者可以通过此 Hook 自定义编辑权限,例如禁用所有文章类型或针对特定文章 ID 或文章类型启用/禁用块编辑器。
关键要点
- 过滤器名称:use_block_editor_for_post
- 参数:$use_block_editor(布尔值,表示文章是否可编辑)和 $post(WP_Post 对象,被检查的文章)
- 返回值:通过 apply_filters 返回布尔值,决定文章是否使用块编辑器
- 引入版本:WordPress 5.0.0
- 相关函数:use_block_editor_for_post() 用于检查文章是否可在块编辑器中编辑
代码示例
// 禁用所有文章类型的块编辑器
add_filter('use_block_editor_for_post', '__return_false');
// 为特定文章 ID 启用块编辑器
function prefix_enable_block_editor_for_post( $use_block_editor, $post ) {
if ( 123 === $post->ID ) {
return true;
}
return $use_block_editor;
}
add_filter( 'use_block_editor_for_post', 'prefix_enable_block_editor_for_post', 10, 2 );
// 禁用特定文章类型的块编辑器
if ( is_admin() ):
add_filter( 'use_block_editor_for_post', 'wpdocs_disable_block_for_post_type', 10, 2 );
endif;
function wpdocs_disable_block_for_post_type( $bool, $post ) {
if ( 'post' === $post->post_type ):
return false;
endif;
return $bool;
}注意事项
- 代码示例应添加到活动主题或子主题的 functions.php 文件中
- 使用过滤器时需注意优先级和参数数量,如 add_filter 中的 10 和 2
- 此过滤器仅影响块编辑器,不影响经典编辑器或其他编辑界面
原文内容
Filters whether a post is able to be edited in the block editor.
Parameters
$use_block_editorbool-
Whether the post can be edited or not.
$postWP_Post-
The post being checked.
Source
return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |
Skip to note 4 content
arifktk
To disable the block editor for all post types, use the following filter:
add_filter('use_block_editor_for_post', '__return_false');Add the above line to your active theme or child theme’s functions.php file.
Skip to note 5 content
Ulrich
function prefix_enable_block_editor_for_post( $use_block_editor, $post ) { // Enable Block Editor for post with ID 123. if ( 123 === $post->ID ) { return true; } return $use_block_editor; } add_filter( 'use_block_editor_for_post', 'prefix_enable_block_editor_for_post', 10, 2 );Skip to note 6 content
kablesh
Disable Gutenberg for certain post type
if ( is_admin() ): add_filter( 'use_block_editor_for_post', 'wpdocs_disable_block_for_post_type', 10, 2 ); endif; function wpdocs_disable_block_for_post_type( $bool, $post ) { if ( 'post' === $post->post_type ): return false; endif; return $bool; }