How to remove custom meta box on click of a button?

江枫思渺然 提交于 2019-12-13 02:56:20

问题


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

回答1:


According to documentation you should use the admin_menu hook




回答2:


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



来源:https://stackoverflow.com/questions/52218102/how-to-remove-custom-meta-box-on-click-of-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!