您可以通过以下两种方式之一进行此操作 -
- 粗暴地删除所有元框,无需任何检查。
- 单独遍历每个元框,这样您就可以检查 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;
}