如何禁用某些帖子类型的古腾堡/块编辑器? [英] How to disable Gutenberg / block editor for certain post types?

查看:30
本文介绍了如何禁用某些帖子类型的古腾堡/块编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WordPress 在其第 5 版中添加了 Gutenberg/块编辑器,并且默认情况下为帖子和页面帖子类型启用.

WordPress added Gutenberg / block editor in its 5th version and it's enabled by default for Post and Page post types.

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

It might be enabled by default for all custom post types in close future so as a WordPress developer I want to know how to disable this editor for my own custom post types? I want to keep classic editor for the post types that I registered from my plugins or themes.

推荐答案

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

It's possible to simply disable the editor using a WordPress filter.

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

If you want to disable the block editor only for your own post types, you can add following code into your plugin or functions.php file of your theme.

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;
}

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

If you want to disable the block editor completely (Not recommended), you can use following code.

add_filter('use_block_editor_for_post_type', '__return_false');

Gutenberg 插件(WordPress 5 之前)

如果您只想为您自己的帖子类型禁用 Gutenberg 编辑器,您可以将以下代码添加到您的插件或主题的 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;
}

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

If you want to disable the Gutenberg editor completely (Not recommended), you can use following code.

add_filter('gutenberg_can_edit_post_type', '__return_false');

这篇关于如何禁用某些帖子类型的古腾堡/块编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆