如何删除自定义 post 类型的所有元数据框?
How do I remove all the metaboxes for a custom post type?
在 Wordpress 中删除特定 post 类型的所有元框的有效方法是什么?
我发现删除元数据框的唯一解决方案似乎是 remove_meta_box 函数,它需要删除元数据框的 ID。我可以像这样删除所有默认的元数据框,这会有点麻烦,但并非不可能,甚至很难。
但是,我将如何始终如一地删除在其他地方添加的元框插件或主题功能?这些是动态的和不可预测的,也许我可以可靠地获得自定义 post 类型编辑页面的元框摘要,然后从那里开始工作?
谢谢,
B
您可以通过以下两种方式之一进行此操作 -
- 在没有任何检查的情况下粗暴地删除所有元数据框。
- 分别循环遍历每个元数据框,这将允许您检查 ID 并根据需要保留一些。
我个人更喜欢方法 2,虽然与更粗暴的方法 1 相比,当然会有一些开销。
使用PHPmicrotime
,速度记录如下-
- 方法 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]);
}
方法二 - 缓慢而小心 -
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;
}
大卫给了你很好的选择。我只是将以下内容用于您的特定用例:(将 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))
);
}
}
然后您可以根据需要添加自己的元框。
在 Wordpress 中删除特定 post 类型的所有元框的有效方法是什么?
我发现删除元数据框的唯一解决方案似乎是 remove_meta_box 函数,它需要删除元数据框的 ID。我可以像这样删除所有默认的元数据框,这会有点麻烦,但并非不可能,甚至很难。
但是,我将如何始终如一地删除在其他地方添加的元框插件或主题功能?这些是动态的和不可预测的,也许我可以可靠地获得自定义 post 类型编辑页面的元框摘要,然后从那里开始工作?
谢谢, B
您可以通过以下两种方式之一进行此操作 -
- 在没有任何检查的情况下粗暴地删除所有元数据框。
- 分别循环遍历每个元数据框,这将允许您检查 ID 并根据需要保留一些。
我个人更喜欢方法 2,虽然与更粗暴的方法 1 相比,当然会有一些开销。
使用PHPmicrotime
,速度记录如下-
- 方法 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]);
}
方法二 - 缓慢而小心 -
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;
}
大卫给了你很好的选择。我只是将以下内容用于您的特定用例:(将 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))
);
}
}
然后您可以根据需要添加自己的元框。