Scott Davies
Scott Davies

Reputation: 65

remove posts, media & comments options from wp-admin dashboard for certain user levels

I have used the below code to hide the posts option in the WP-ADMIN section of my site, but obviously that's hiding it even for admin staff. I'd like to remove that option for everyone except admin staff. I only actually use admin and author groups... does anyone know if it is possible to remove the option only for certain user types?

I'd also like to remove the media and comments options, too.

function remove_posts_menu() 
{
    remove_menu_page('edit.php');
} 

This is being used for a site where we want authors to ONLY be able to submit posts to 2 specific categories, so I've added those 2 categories to the menu, but now want to remove all the other options because, well... people don't listen to instructions sometimes lol.

Thank you!

Upvotes: 0

Views: 574

Answers (1)

disinfor
disinfor

Reputation: 11558

You can use the wp_get_current_user() for this:

function remove_posts_menu() 
{
    // Get the current user Object.
    $current_user = wp_get_current_user();
    // Check if the administrator role is NOT in the roles property array
    if ( ! in_array( 'administrator', $current_user->roles ) ) :
        // remove any other stuff here - something like this.
        remove_menu_page('edit.php');
        // Media
        remove_menu_page('upload.php');
        // Comments
        remove_menu_page('edit-comments.php');
    endif;
} 

add_action('admin_init', 'remove_posts_menu');

This hasn't been tested, but the main part is checking for the user role.

Here's some docs on the remove_menu_page()

Upvotes: 0

Related Questions