如何禁用古腾堡/阻止某些帖子类型的编辑器?

WordPress在其第5版中添加了Gutenberg / block editor,默认情况下为帖子和页面帖子类型启用。

在不久的将来,它可能会默认为所有自定义帖子类型启用,因此作为WordPress开发人员,我想知道如何为我自己的自定义帖子类型禁用此编辑器?我想保留我从插件或主题注册的帖子类型的经典编辑器。


答案 1

可以使用WordPress过滤器简单地禁用编辑器。

WordPress 5 及更高版本

如果您只想为自己的帖子类型禁用块编辑器,则可以将以下代码添加到主题的插件或文件中。functions.php

add_filter('use_block_editor_for_post_type', 'prefix_disable_gutenberg', 10, 2);
function prefix_disable_gutenberg($current_status, $post_type)
{
    // Use your post type key instead of 'product'
    if ($post_type === 'product') return false;
    return $current_status;
}

如果要完全禁用块编辑器(不推荐),可以使用以下代码。

add_filter('use_block_editor_for_post_type', '__return_false');

Gutenberg Plugin (在 WordPress 5 之前)

如果您只想为自己的帖子类型禁用古腾堡编辑器,则可以将以下代码添加到主题的插件或文件中。functions.php

add_filter('gutenberg_can_edit_post_type', 'prefix_disable_gutenberg', 10, 2);
function prefix_disable_gutenberg($current_status, $post_type)
{
    // Use your post type key instead of 'product'
    if ($post_type === 'product') return false;
    return $current_status;
}

如果要完全禁用古腾堡编辑器(不推荐),可以使用以下代码。

add_filter('gutenberg_can_edit_post_type', '__return_false');

答案 2

正如上面显示的其他用户一样,是的。另外,我想让以下事情知道。

在最新的Wordpress或Wordpress 5 + - (使用古腾堡)中,这两种方法对删除古腾堡具有相同的效果,但在这样做时也有不同的选择:

(将两者都插入函数.php或自定义插件函数)

要从帖子类型中删除古腾堡:

add_filter('use_block_editor_for_post_type', 'prefix_disable_gutenberg', 10, 2);

 function prefix_disable_gutenberg($gutenberg_filter, $post_type)
  {
   if ($post_type === 'your_post_type') return false;
   return $gutenberg_filter;
  }

以上内容将从您的自定义帖子类型中完全删除Gutenberg编辑器,但也保留其他元框/等可用且未更改。

但是,如果您希望删除文本编辑器/文本区域本身 - 或其他默认选项,WordPress也会将其视为古腾堡,因此您可以通过添加以下内容来专门删除它并同时删除古腾堡:

add_action('init', 'init_remove_editor',100);

 function init_remove_editor(){
  $post_type = 'your_post_type';
  remove_post_type_support( $post_type, 'editor');
 }

以上将禁用Gutenberg和wordpress的“编辑器”。这可以替换为其他元框/数据选项。(作者/缩略图/修订等)


推荐