How to remove WordPress post meta boxes

The WordPress post screen contains a number of meta boxes that allow you to do things with your posts. For example, you can select the post category in the Categories meta box, and you can control the comments setting for individual posts in the Discussion meta box. But, what if you want to prevent some users from changing these things? No problem. You can disable any of the meta boxes with a few lines of code.

Let’s say you want to prevent users with the Contributors and Authors roles from disabling comments on a post. Worded another way, you only want users with the edit_others_posts capability to be able to disable comments. Here’s how you can do that.

Heads up
At this point it’s worth noting that we’re talking about a WordPress site using the default user roles of Subscriber, Contributor, Author, Editor, and Administrator. If you have custom roles, you may need to adjust the code to fit your needs.
/**
 * Remove the Discussion meta box for users without
 * the 'edit_others_posts' capability.
 */
function jp_remove_discussion_post_meta_box() {

  if ( !current_user_can( 'edit_others_posts' ) ) {
    remove_meta_box( 'commentstatusdiv', 'post', 'normal' );
  }

}
add_action( 'admin_menu', 'jp_remove_discussion_post_meta_box' );

Or let’s say you want to prevent users from adding tags to a post. You can disable the Tags meta box with the following code.

/**
 * Remove the Tags meta box for all users except administrators
 */
function jp_remove_tags_post_meta_box() {

  if ( !current_user_can( 'administrator' ) ) {
    remove_meta_box( 'tagsdiv-post_tag', 'post', 'normal' );
  }

}
add_action( 'admin_menu', 'jp_remove_tags_post_meta_box' );

WordPress has a way to disable all of the meta boxes. Here’s a list of the meta boxes and the HTML id attribute needed to remove them.

Author - 'authordiv'
Category - 'categorydiv'
Comments - 'commentsdiv'
Discussion - 'commentstatusdiv'
Formats - 'formatdiv'
Attributes - 'pageparentdiv'
Custom fields - 'postcustom'
Excerpt - 'postexcerpt'
Featured Image - 'postimagediv'
Revisions - 'revisionsdiv'
Slug - 'slugdiv'
Publish - 'submitdiv'
Tags - 'tagsdiv-post_tag'
Trackbacks - 'trackbacksdiv'

WordPress offers a number of capabilities you can target for controlling your site. Check out the Roles and Capabilities page for all the details.

Leave a Reply