How to disable Gutenberg / block editor for certain post types?

前端 未结 3 1489
旧时难觅i
旧时难觅i 2021-02-01 10:40

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

It might be enabled by default fo

相关标签:
3条回答
  • 2021-02-01 11:06

    Another way if you use custom post type.

    When you register a cpt add add_post_type_support( 'news', 'excerpt' );

    Full example :

    function create_news() {
        $args = [
            'labels' => [
                'name' => __( 'News', 'lang' ),
                'singular_name' => __( 'News', 'lang' ),
                'add_new_item'       => __( 'Add a news', 'lang' ),
        ],
            'public' => true,
            'has_archive' => true,
            'menu_icon' => 'dashicons-admin-post',
            'show_in_rest' => false,
            'rewrite' => ['slug' => 'news'],
            'show_in_nav_menus' => true,
        ];
    
        register_post_type( 'news',
            $args
        );
    }
    add_action( 'init', 'create_news' );
    add_post_type_support( 'news', 'excerpt' );
    
    0 讨论(0)
  • 2021-02-01 11:07

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

    WordPress 5 and Higher

    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 Plugin (Before WordPress 5)

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

    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');
    
    0 讨论(0)
  • 2021-02-01 11:26

    as the other users shown above it is possible yes. Also, i'd like to make the following known.

    In the latest Wordpress or Wordpress 5+ - (With Gutenberg) The 2 methods have the same effect to removing Gutenberg but also have different options when doing so:

    (Insert both to functions.php or custom plugin functions)

    To remove Gutenberg from your post type:

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

    The above will remove Gutenberg editor completely from your custom post type but also leave other meta boxes/etc available and untouched.

    However, if you wish to remove the text editor/text area itself - or other default options WordPress also considers this as Gutenberg, so you can remove this specifically and remove Gutenberg at the same time by adding the following:

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

    The above will disable Gutenberg & the 'editor' of wordpress. This can replaced with other metabox/data options. (Author / Thumbnail /Revisions etc)

    0 讨论(0)
提交回复
热议问题