How to add or remove metabox each page separately?

How to add a meta box for a particular page, say: for only About Us page.

How shall i do?

Read More

The current code adds the metaboxes for all pages:

function myplugin_add_custom_box() {
    add_meta_box( 
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ),
        'myplugin_inner_custom_box',
        'post' 
    );
    add_meta_box(
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ), 
        'myplugin_inner_custom_box',
        'page'
    );
}

Related posts

Leave a Reply

2 comments

  1. Here is a function that you can pass either a single post id or an array of ids. Call this function in your function that adds the meta boxes. If the id or ids don’t match the meta boxes will not display on that post or page.

    function check_id( $id ) {
        // Get the current post ID
        if ( isset( $_GET[ 'post' ] ) ) $post_id = $_GET[ 'post' ];
        elseif ( isset( $_POST[ 'post_ID' ] ) ) $post_id = $_POST[ 'post_ID' ];
            if ( ! isset( $post_id ) )
                return false;
        // If value isn't an array, turn it into one
        $id = ! is_array ( $id ) ? array ( $id ) : $id;
    
        // If current page id is in the included array, display the metabox
        if ( in_array ( $post_id, $id ) )
            return true;
        else
            return false;
    }
    
     function myplugin_add_custom_box() {
        $what_page = check_id( array( 3, 18, 15 ) );
        if ( false == $what_page ) return;
        add_meta_box( 
            'myplugin_sectionid',
            __( 'My Post Section Title', 'myplugin_textdomain' ),
            'myplugin_inner_custom_box',
            'post' 
        );
        add_meta_box(
            'myplugin_sectionid',
            __( 'My Post Section Title', 'myplugin_textdomain' ), 
            'myplugin_inner_custom_box',
            'page'
        );
    }
    
  2. Here’s another possibility: show or hide the Meta Box according to the page template or to the post category.

    • page-wpse-53486.php is the template filename.
    • form#adv-settings label[for='myplugin_sectionid-hide is the meta box option inside the Screen options. We don’t want it to appear if the meta box is not available for the post/page.
    • #in-category-6 is the checkbox for the category with ID 6.
    add_action('admin_head', 'wpse_53486_script_enqueuer');
    function wpse_53486_script_enqueuer() {
        global $current_screen;
        if('page' == $current_screen->id) 
        {
            echo <<<HTML
                <script type="text/javascript">
                jQuery(document).ready( function($) {
    
                    /**
                     * Adjust visibility of the meta box at startup
                    */
                    if($('#page_template').val() == 'page-wpse-53486.php') {
                        // show the meta box
                        $('#myplugin_sectionid').show();
                        $("form#adv-settings label[for='myplugin_sectionid-hide']").show();
                    } else {
                        // hide your meta box
                        $('#myplugin_sectionid').hide();
                        $("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
                    }
    
                    // Debug only
                    // - outputs the template filename
                    // - checking for console existance to avoid js errors in non-compliant browsers
                    if (typeof console == "object") 
                        console.log ('default value = ' + $('#page_template').val());
    
                    /**
                     * Live adjustment of the meta box visibility
                    */
                    $('#page_template').live('change', function(){
                            if($(this).val() == 'page-wpse-53486.php') {
                            // show the meta box
                            $('#myplugin_sectionid').show();
                            $("form#adv-settings label[for='myplugin_sectionid-hide']").show();
                        } else {
                            // hide your meta box
                            $('#myplugin_sectionid').hide();
                            $("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
                        }
    
                        // Debug only
                        if (typeof console == "object") 
                            console.log ('live change value = ' + $(this).val());
                    });                 
                });    
                </script>
    HTML;
        } 
        elseif ( 'post' == $current_screen->id ) 
        {
            echo <<<HTML
                <script type="text/javascript">
                jQuery(document).ready( function($) {
                    if ( $('#in-category-6').is(':checked') ) {
                        $("form#adv-settings label[for='myplugin_sectionid-hide']").show();
                        $('#myplugin_sectionid').show();
                    } else {
                        $('#myplugin_sectionid').hide();
                        $("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
                    }
    
                    $('#in-category-6').live('change', function(){
                        if ( $(this).is(':checked') ) {
                            $('#myplugin_sectionid').show();
                            $("form#adv-settings label[for='myplugin_sectionid-hide']").show();
                        } else {
                            $('#myplugin_sectionid').hide();
                            $("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
                        }
                    });                 
                });    
                </script>
    HTML;
        }
    }