【问题标题】:How to disable Gutenberg / block editor for certain post types?如何为某些帖子类型禁用古腾堡/块编辑器?
【发布时间】:2019-02-11 10:55:16
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案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 插件(在 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');
    

    【讨论】:

    • 此过滤器已重命名为 use_block_editor_for_post_type
    • 过滤器必须有变量计数(在我的情况下,没有它,我有错误 500)。它适用于:add_filter('use_block_editor_for_post_type', 'prefix_disable_gutenberg', 10, 2)
    • 出于某种原因,post_type 参数值是'post',而不是特定 cpt 帖子类型的正确 id(或处理程序)名称。现在我的 3 种 cpt 类型中有 2 种有这个问题。 (我清除了我所有的自定义函数,所以没有一个会产生这个问题)。有什么想法吗?
    【解决方案2】:

    正如上面显示的其他用户,可能是的。另外,我想让大家知道以下几点。

    在最新的 Wordpress 或 Wordpress 5+ - (With Gutenberg) 这两种方法对删除 Gutenberg 的效果相同,但这样做时也有不同的选择:

    (同时插入functions.php或自定义插件函数)

    要从您的帖子类型中删除 Gutenberg:

    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 也将其视为 Gutenberg,因此您可以通过添加以下内容专门删除它并同时删除 Gutenberg:

    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 的“编辑器”。这可以用其他元框/数据选项替换。 (作者/缩略图/修订等)

    【讨论】:

      【解决方案3】:

      如果您使用自定义帖子类型,则另一种方式。

      注册cpt时添加add_post_type_support( 'news', 'excerpt' );

      完整示例:

      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' );
      

      【讨论】:

        【解决方案4】:

        由于您在插件中注册自定义帖子类型,禁用块编辑器的最快解决方案是将register_post_type 中的选项show_in_rest 设置为false:

        <?php
        $args = array(
          'label'        => 'Custom Posts',
          'show_ui'      => true,
          'show_in_rest' => false,          // ← Disables the block editor.
        );
        register_post_type( 'my-cpt-slug', $args );
        

        【讨论】:

          【解决方案5】:

          如果您想从自定义帖子类型中删除编辑器或任何其他字段,请在 functions.php 文件中使用此代码

          add_action( 'init', function() {
              remove_post_type_support( 'custom post name', 'filed name' );
          }, 99);
          

          【讨论】:

            猜你喜欢
            • 2022-01-09
            • 1970-01-01
            • 1970-01-01
            • 2020-11-05
            • 2019-10-24
            • 2019-10-04
            • 2019-11-30
            • 2021-03-27
            • 2018-06-20
            相关资源
            最近更新 更多