How to remove meta boxes and fields based on user roles

Assigning a role to each user is a great way of controlling user behavior on your WordPress website. By default, there are specific roles which can or cannot do a certain thing on your blog. You can change user role names easily and you can even create new roles to fit your needs. But sometimes, even that isn’t enough and you will want to remove specific content for specific users.

Instead of looking for a plugin which will create exactly what you need, you can define user behavior with some minor changes to your functions.php file.

You will need to find the meta-box or a field which you want to remove for a specific user role. After you define a new rule in theme files, only users with a higher level of authority will be able to see the element you have chosen in the code. Without further ado, let’s see the code and modify the user behavior.

Remove administration meta boxes:

  1. Open functions.php file
  2. Copy and paste the following code:
  3. add_action('admin_init','customize_meta_boxes');
    function customize_meta_boxes() {
    global $current_user;
    get_currentuserinfo();
    if ( !current_user_can( 'manage_options' ) ) {
    remove_meta_box('postcustom','post','normal');
    }
  4. Make the changes to user level and the meta-box (explained below)
  5. Save changes

OK, now that the code is ready, you need to modify two parts of the code in order to achieve different results for your site.

First, you will have to decide which user roles can/can’t see the meta-box you’re about to define later. You can select any users you want by simply checking user capabilities. By selecting specific capability you can easily include or exclude certain user roles for which the meta-box will be removed.

In the example above, we have checked if a user can manage options, i.e. if the user is an administrator. If so, WordPress will remove a meta-box which is defined in the line below.

So, after you have selected users which are going to be affected by the code, you need to tell your system which meta-box it needs to remove.

Author meta box

There are three parts you need to include in order to remove_meta_box function. You will need ID of the box, then you need to choose where the box will be removed (post, page or link) while the last one determines the position within its context (normal or advanced).

To find out the box ID, you should use the “Inspect Element” feature of the modern browser. To make things a bit clearer, you can follow our example of removing the “Author” box from all posts:

  1. Go to Posts->All Posts
  2. Choose any post and edit it
  3. Navigate to “Author” box where you can usually select author of the current post
  4. Right click on the box and click on “Inspect Element” if you’re using Chrome or Firefox
  5. A console will open where you need to find DIV ID

In this example, the console told us that the Author box ID is “authordiv” which actually makes sense. We found out that from the following line of HTML code:

<div id="authordiv" class="postbox">

Now that you have found meta-box ID, you have the most important parameter of aforementioned three parts and you’re ready to modify the original code:

remove_meta_box('authordiv','post','normal');

This code will tell WordPress to remove meta-box with “authordiv” ID, on all post pages.

Following the same logic, you can choose any meta box that you want to hide and play around with it until you achieve results that you want.

We hope this code helped you control user behavior in details. If it’s not a secret, which box did you hide from your users and why?

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *