Hide Meta Boxes for Non-Admins

I have a review based wordpress site where I have a page for each different company where there users can review them. The theme I use adds a custom post type called “review” and I have used Press Permit to create limited user accounts that can modifying some parts of these review pages.

I wanted to hide some of the Meta Boxes that they should not have access to such as Yoast SEO, GD Star Ratings, etc. I followed the instructions from some other posts on this website, however it does not hide the meta boxes as it should.

I have added the following code at the end of the “functions.php” in my Theme directory as per the instructions at WordPress SEO by Yoast: Hide Meta boxes in posts for non-admins

add_action('add_meta_boxes', 'hide_yoastseo_metabox', 99);

function hide_yoastseo_metabox()
{
    if (!current_user_can('activate_plugins'))
    {
        remove_meta_box('wpseo_meta', 'review', 'normal');
    }
}

2 Answers
2

Does lowering the priority of your add_action() call here help? This was mentioned by Kid Slimmer and Piet in the very post you linked to you, in this comment → https://wordpress.stackexchange.com/a/91184/33604

Their code example uses a priority of 10000 instead of 99, like you’re using – try updating your priority to 500 or 1000 or something to begin with, and see if anything changes.

You also may find better luck by checking user capabilities before adding the action itself, which was also demonstrated in the comment I linked to above.

So, for example, something like this might work a little better:

if( ! current_user_can('activate_plugins') ) {
    add_action('add_meta_boxes', 'hide_yoastseo_metabox', 1000);
}

function hide_yoastseo_metabox() {
    remove_meta_box('wpseo_meta', 'review', 'normal');
}

Other than these recommendations, the only other thing I could think of would be to double-check all your values for remove_meta_box() – make sure 'wpseo_meta' is the proper metabox id for the Yoast metabox you’re trying to remove, 'review' is the proper post type that you want to hide the metabox on, and 'normal' is the proper priority for the metabox you’re targeting. You can read more about remove_meta_box() here.

Also, not to insult your intelligence here, but I’m curious: are you accessing your “review” post type edit screens in different capability levels? If you’re only viewing things in your admin from your full-capability superadmin account, then code that only applies to limited-capability accounts won’t run for you. Make sure you’ve got a test account with the limited capabilities you’re testing, and try viewing things from within that account.

Leave a Comment