限制自定义 post 类型 wordpress 的古腾堡编辑器
Restrict Gutenberg Editor for custom post type wordpress
我需要创建自己的自定义 post 类型,并希望限制我的 post 类型的 Gutenberg 编辑器(仅)并在此 post 类型中使用 WordPress 编辑器。
如何为我的 cpt 限制此插件?
谢谢
您可以使用过滤器为所有 post 类型禁用 Gutenberg,自定义 post 类型名称除外。
/**
* Disabling the Gutenberg editor all post types except post.
*
* @param bool $can_edit Whether to use the Gutenberg editor.
* @param string $post_type Name of WordPress post type.
* @return bool $can_edit
*/
function gutenberg_can_edit_post_type_83744857( $can_edit, $post_type ) {
$gutenberg_supported_types = array( 'post' ); //Change this to you custom post type
if ( ! in_array( $post_type, $gutenberg_supported_types, true ) ) {
$can_edit = false;
}
return $can_edit;
}
add_filter( 'gutenberg_can_edit_post_type', 'gutenberg_can_edit_post_type_83744857', 10, 2 );
您可以通过插件或自定义代码执行此操作。
代码,添加到functions.php
function mh_disable_gutenberg($is_enabled, $post_type) {
if ($post_type === 'news') return false; // change news to your post type
return $is_enabled;
}
add_filter('gutenberg_can_edit_post_type', 'mh_disable_gutenberg', 10, 2);
对于那些在 WordPress 5.0 发布后发现此问题的人,您可以使用 use_block_editor_for_post_type 像这样过滤:
add_filter('use_block_editor_for_post_type', function( $useBlockEditor, $postType ){
if( $postType == 'your-custom-post-type-slug' )
return false;
return $useBlockEditor;
}, 10, 2);
我需要创建自己的自定义 post 类型,并希望限制我的 post 类型的 Gutenberg 编辑器(仅)并在此 post 类型中使用 WordPress 编辑器。 如何为我的 cpt 限制此插件?
谢谢
您可以使用过滤器为所有 post 类型禁用 Gutenberg,自定义 post 类型名称除外。
/**
* Disabling the Gutenberg editor all post types except post.
*
* @param bool $can_edit Whether to use the Gutenberg editor.
* @param string $post_type Name of WordPress post type.
* @return bool $can_edit
*/
function gutenberg_can_edit_post_type_83744857( $can_edit, $post_type ) {
$gutenberg_supported_types = array( 'post' ); //Change this to you custom post type
if ( ! in_array( $post_type, $gutenberg_supported_types, true ) ) {
$can_edit = false;
}
return $can_edit;
}
add_filter( 'gutenberg_can_edit_post_type', 'gutenberg_can_edit_post_type_83744857', 10, 2 );
您可以通过插件或自定义代码执行此操作。
代码,添加到functions.php
function mh_disable_gutenberg($is_enabled, $post_type) { if ($post_type === 'news') return false; // change news to your post type return $is_enabled; } add_filter('gutenberg_can_edit_post_type', 'mh_disable_gutenberg', 10, 2);
对于那些在 WordPress 5.0 发布后发现此问题的人,您可以使用 use_block_editor_for_post_type 像这样过滤:
add_filter('use_block_editor_for_post_type', function( $useBlockEditor, $postType ){
if( $postType == 'your-custom-post-type-slug' )
return false;
return $useBlockEditor;
}, 10, 2);