如何删除自定义职位类型的所有metaboxes?

问题描述 投票:1回答:2

什么是消除所有的元盒在WordPress的特定支柱式结构的有效途径?

在所有的,我已经找到了消除metaboxes唯一的解决办法似乎是remove_meta_box功能,这就要求要删除的metabox的ID。我可以删除所有默认metaboxes这样,这将是一个有点繁琐,但并非不可能,甚至硬。

但是,我怎么会去一直删除添加的插件或主题功能别处元盒?这些都是动态的,不可预测的,也许我可以得到可靠的元盒汇总自定义文章类型编辑页面,也许从那里工作?

谢谢,B

php wordpress
2个回答
3
投票

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

  1. 惨遭删除所有metaboxes没有任何形式的检查。
  2. 通过每个单独的metabox,这将允许你检查ID和保留一些,如果你想循环。

我个人的偏好是方法2,虽然会有,当然会出现一些开销相比,更残酷的方法1。

使用PHP microtime的速度记录如下 -

  • 方法1 - 0(非常非常快)。
  • 方法2 - 0.00099992752075195(相当不错的快,但明显慢)。

方法1 - 快速和残酷的 -

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

}

方法2 - 缓慢和谨慎 -

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;

}

0
投票

大卫已经给了你很好的选择。我只是用你的具体使用情况如下:(与你的CPT的名称替换your_custom_post_type)

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

然后,只要你喜欢,你可以添加自己的中继盒。

© www.soinside.com 2019 - 2024. All rights reserved.