How to remove custom meta box on click of a button

2019-08-21 05:05发布

I'm trying to remove a custom metabox that I've created for my plugin using PHP code. It should be removed from all the posts on click of a button. Here is my code:

<?php
if(isset($_REQUEST['submit_btn']))
{   
    function remove_custom_metabox()
    {
        remove_meta_box( 'my-meta-box-id' , 'post' , 'normal' );
    }
    add_action( 'add_meta_boxes', 'remove_custom_metabox');
}
?>

Why is it not working? And is there any way to do this for multi-post custom meta-box as well? Thanks!

EDIT 1: Just to get more clear idea of what I'm doing, here is how I'm creating the custom meta-box in the main plugin file:

function cd_meta_box_add()
{
        add_meta_box(
                'my-meta-box-id', //id
                'Contributors', //title
                'cd_meta_box_cb', //callback
                'post', //post type
                'normal', //position
                'high' //priority
                );
}
add_action('add_meta_boxes', 'cd_meta_box_add');

2条回答
Lonely孤独者°
2楼-- · 2019-08-21 05:35

To remove custom meta box use action hook admin_menu or do_meta_boxes

/**
 * Remove Custom Fields meta box
 */
function wpdocs_remove_post_custom_fields() {
    remove_meta_box( 'postcustom' , 'post' , 'normal' ); 
}
add_action( 'admin_menu' , 'wpdocs_remove_post_custom_fields' );
add_action( 'do_meta_boxes', 'wpdocs_remove_post_custom_fields' );

For more help see this link : click here

查看更多
劫难
3楼-- · 2019-08-21 05:57

According to documentation you should use the admin_menu hook

查看更多
登录 后发表回答