【问题标题】:How do I remove all the metaboxes for a custom post type?如何删除自定义帖子类型的所有元框?
【发布时间】:2019-02-03 15:50:42
【问题描述】:

在 Wordpress 中删除特定帖子类型的所有元框的有效方法是什么?

我发现的唯一删除元框的解决方案似乎是 remove_meta_box 函数,它需要删除元框的 id。我可以像这样删除所有默认的元框,这会有点繁琐,但并非不可能,甚至很难。

但是,我将如何持续删除在其他地方添加插件或主题功能的元框?这些是动态且不可预测的,也许我可以可靠地获得自定义帖子类型编辑页面的元框摘要,并且可以从那里开始工作?

谢谢, 乙

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以通过以下两种方式之一进行此操作 -

    1. 粗暴地删除所有元框,无需任何检查。
    2. 单独遍历每个元框,这样您就可以检查 ID 并根据需要保留一些。

    我个人更喜欢方法 2,尽管与更残酷的方法 1 相比当然会有一些开销。

    使用PHPmicrotime,速度记录如下-

    • 方法 1 - 0(非常非常快)。
    • 方法 2 - 0.00099992752075195(非常快,但显然更慢)。

    方法一 - 快速而残酷 -

    add_action('add_meta_boxes', 'my_remove_meta_boxes1', 99, 2);
    function my_remove_meta_boxes1($post_type, $post){
    
        global $wp_meta_boxes;
    
        /** Simply unset all of the metaboxes, no checking */
        unset($wp_meta_boxes[$post_type]);
    
    }
    

    方法二 - 慢而小心 -

    add_action('add_meta_boxes', 'my_remove_meta_boxes2', 99, 2);
    function my_remove_meta_boxes2($post_type, $post){
    
        /** Check the post type (remove if you don't want/need) */
        if(!in_array($post_type, array(
                                     'post',
                                     'page'
                                 ))) :
            return false;
        endif;
    
        global $wp_meta_boxes;
    
        /** Create an array of meta boxes exceptions, ones that should not be removed (remove if you don't want/need) */
        $exceptions = array(
            'postimagediv'
        );
    
        /** Loop through each page key of the '$wp_meta_boxes' global... */
        if(!empty($wp_meta_boxes)) : foreach($wp_meta_boxes as $page => $page_boxes) :
    
                /** Loop through each contect... */
                if(!empty($page_boxes)) : foreach($page_boxes as $context => $box_context) :
    
                        /** Loop through each type of meta box... */
                        if(!empty($box_context)) : foreach($box_context as $box_type) :
    
                                /** Loop through each individual box... */
                                if(!empty($box_type)) : foreach($box_type as $id => $box) :
    
                                        /** Check to see if the meta box should be removed... */
                                        if(!in_array($id, $exceptions)) :
    
                                            /** Remove the meta box */
                                            remove_meta_box($id, $page, $context);
                                        endif;
    
                                    endforeach;
                                endif;
    
                            endforeach;
                        endif;
    
                    endforeach;
                endif;
    
            endforeach;
        endif;
    
    }
    

    【讨论】:

      【解决方案2】:

      David 为您提供了不错的选择。我只是将以下内容用于您的特定用例:(将 your_custom_post_type 替换为您的 CPT 的名称)

      add_action( 'add_meta_boxes', 'test_remove_metaboxes', 5 ); // hook early and remove all metaboxes
      
      function test_remove_metaboxes(){
          global $wp_meta_boxes;
          global $post;
          $current_post_type = get_post_type($post);
          if($current_post_type == 'your_custom_post_type') {
              $publishbox = $wp_meta_boxes['your_custom_post_type']['side']['core']['submitdiv'];
              $wp_meta_boxes = array();
              $wp_meta_boxes['your_custom_post_type'] = array(
                  'side' => array('core' => array('submitdiv' => $publishbox))
                );
          }
      }
      

      然后您可以根据需要添加自己的元框。

      【讨论】:

      • 简单并且完成了工作。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      • 2022-01-25
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      相关资源
      最近更新 更多