Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to remove some of the admin features for a user with the role of contributor. What i mean by remove some of the admin features is disable them from seeing certain admin menu items, such as comments, tools, media ect. I have managed to remove the items I want from the admin menu, using this code:

function remove_menus(){

$author = wp_get_current_user();
if(isset($author->roles[0])){ 
    $current_role = $author->roles[0];
}else{
    $current_role = 'no_role';
}

if($current_role == 'contributor'){  
  remove_menu_page( 'index.php' );                  //Dashboard
  remove_menu_page( 'edit.php' );                   //Posts
  remove_menu_page( 'upload.php' );                 //Media
  remove_menu_page( 'tools.php' );                  //Tools
  remove_menu_page( 'edit-comments.php' );               //Comments

}

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

It works a treat. The problem I am facing is that I can just manually add the query string to the url, eg /wp-admin/edit.php and that will take me to the post edit screen. Does anyone know a way to restrict these pages from being accessed altogether, as well as hiding them from the admin menu?

share|improve this question
    
Exactly what do you want this user to be able to do? It sounds more like you need a custom role rather than trying to hack the contributor role the way you are. –  s_ha_dum Apr 26 at 15:09
    
I want the user to be able to edit their profile and manage their own posts of a custom post type. I don't really think I need a custom role, as the contributor one is doing what I need just fine. I just need a way of stopping certain pages from being viewed in the admin area. If i create a custom role and give them the ability to manage their own posts, surely the same problem would occur? –  RoseCoder Apr 26 at 16:45

3 Answers 3

I typically use (and recommend) the Members plugin. The UI is very easy to use, and you have the added benefit of storing your role/permission logic in the database (rather than the code) which makes "on-the-fly" changes easy to do.

This plugin allows you to even create a custom role (maybe you want to invent one called "Photo Editor" or "Comment Approver" etc) and limit which menus they may see & access. I think this will do exactly what you want.

share|improve this answer

You could create a custom role as suggested by s_da_hum or remove capabilities from the contributor role by adding this code to your child themes functions.php file.

add_action( 'init', 'wpsites_remove_contributor_capabilities' );

function wpsites_remove_contributor_capabilities() {

$contributor = get_role( 'contributor' );

$caps = array(
    'edit_posts',
    'delete_posts',
);

foreach ( $caps as $cap ) {

    $contributor->remove_cap( $cap );
    }
}

Source https://codex.wordpress.org/Function_Reference/add_role

share|improve this answer

Thanks for the answers guys, but I didn't need a new custom role. I figured it out in the end, incase anybody else needs to know, this is the code I used:

function restrict_menus(){

$author = wp_get_current_user();
if(isset($author->roles[0])){ 
    $current_role = $author->roles[0];
}else{
    $current_role = 'no_role';
}

if($current_role == 'contributor'){  

    $screen = get_current_screen();
    $base = $screen->id;


    if($base == 'edit-post' || $base == 'upload' || $base == 'tools' || $base == 'edit-comments'){
        wp_die('Cheatin’ uh?');
    }
}

}

add_action( 'current_screen', 'restrict_menus' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.