Hiding a theme’s meta box

Remove WordPress Meta Boxes

You have to make use of the function called remove meta box.

Description:
Removes a meta box or any other element from a particular post edit screen of a given post type. It can be also used to remove a widget from the dashboard screen.

Steps to remove the meta box from WordPress Dashboard:

When removing WordPress meta boxes, I’ve seen it done in a number of different ways two of which are often done via JavaScript. Though this is functional for many, client-side solutions aren’t always great especially as it relates to accessibility.

On top of that, hiding things via JavaScript is kind of a hack – the visibility of elements should often be set in terms of CSS and it will likely cause a little bit of a flicker when the element is being hidden while the DOM loads.

Anyway, the two ways I’ve often seen it done is when:

Developers look for the check boxes under the Screen Options and then trigger the click event.

Developers sniff out the meta boxes ID and then use jQuery’s hide() method to hide the meta boxes from the page.

Sure, these work, but you’re still faced with the challenges of accessibility and with the fact that you’re modifying something that should probably be done via CSS.

Unless a hook exists.

And that’s really the mentality that we, as WordPress developers, should have whenever we’re attempting to introduce, remove, or modify anything as it relates to WordPress both in the dashboard and on the front-end.

Specifically, we need to ask ourselves:

Given what I need to do, is there a hook that makes this available?

And this is this case (and many, many cases), there is. We can take advantage of the default_hidden_meta_boxes hook.

Using The Hook

The function accepts two arguments:

  1. An array of meta boxes that should be hidden
  2. The current screen being displayed

This allows us to modify the existing array of hidden meta boxes (or create our own) and only trigger it on specific screen’s, as well.

So let’s say that we have a custom post type called acme_post_type and we want to hide the Categories, Author, Post Excerpt, and Slug meta box.

<?php
add_action( 'default_hidden_meta_boxes', 'acme_remove_meta_boxes', 10, 2 );
/**
 * Removes the category, author, post excerpt, and slug meta boxes.
 *
 * @since    1.0.0
 *
 * @param    array    $hidden    The array of meta boxes that should be hidden for Acme Post Types
 * @param    object   $screen    The current screen object that's being displayed on the screen
 * @return   array    $hidden    The updated array that removes other meta boxes
 */
function acme_remove_meta_boxes( $hidden, $screen ) {
    if ( 'acme_post_type' == $screen->id ) {
        $hidden = array(
            'acme_post_type_categorydiv',
            'authordiv',
            'postexcerpt',
            'slugdiv'
            );

    }
    return $hidden;    
}

First, the code checks to see if we’re on the screen for the custom post type. If so, then we define a new array that includes the IDs of the meta boxes that we want to hide. After that, we the array to WordPress and the end-result of the function will be that the specified meta boxes will be hidden from view.

In terms of getting the ID of the meta boxes that you want to hide, you can use the Developer Tools of your preferred browser to inspect the elements on the page.

Another Example of how to remove the meta box in WordPress.

// REMOVE POST META BOXES
function remove_my_post_metaboxes() {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Status Metabox
remove_meta_box( 'commentsdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'revisionsdiv','post','normal' ); // Revisions Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Trackback Metabox
}
add_action('admin_menu','remove_my_post_metaboxes');

Place the following code in your functions file (Appearance > Editor > Theme Functions – functions.php).

Placing that code should remove all the meta boxes below the post editor and give you a nice, clean look.

Leave a Comment