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?

4 Answers
4

I figured it out in the end and 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( 'contributor' == $current_role ) {  
        $screen = get_current_screen();
        $base   = $screen->id;


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

Leave a Reply

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