php - 如何禁用某些帖子类型的古腾堡/ block 编辑器?

标签 php wordpress rich-text-editor wordpress-gutenberg wp-editor

WordPress 在其第五版中添加了古腾堡/ block 编辑器,并且默认情况下为帖子和页面帖子类型启用。

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

最佳答案

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

WordPress 5 及更高版本

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

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

add_filter('use_block_editor_for_post_type', '__return_false');

古腾堡插件(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');

关于php - 如何禁用某些帖子类型的古腾堡/ block 编辑器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52199629/

相关文章:

visual-studio-code - 是否可以在 Visual Studio Code 中将文本切换为删除线文本?

PHP Regex - 最少 8 个字符,1 个字母,1 个数字

javascript - 使用 PHP 将带有 < 和 > 运算符的字符串插入数据库

php - 如何计算某个meta_key在列中出现meta_value的次数?

wordpress - 如何按 View 、评论、评级等 WordPress 对帖子进行排序

javascript - CKEditor:调用 editor#setData 后恢复插入位置

php - 为 curl 请求选择传出 IP

php - 一次又一次地使用 XAMPP 和 Windows 获取错误 "An operation on a socket could not be performed..."

javascript - 每 N 次迭代创建单元格并包装在父级中

html - 用于自定义博客的良好富文本编辑器