WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I did try using the plugin Front End Users but this clashes with something as it prevents access to some front end pages. So I need to manually just set it so that anyone who isn't one of two usernames (or roles) can't access wp-admin.

share|improve this question
up vote 7 down vote accepted


It's basically just a user capability check, followed by a redirect in an exit call. It then redirects to the site the request came from.

! defined( 'ABSPATH' ) AND exit;
/* Plugin Name: (#66093) »kaiser« Deny Admin-UI access for certain roles */

function wpse66093_no_admin_access()
    $redirect = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : home_url( '/' );
    if ( 
        current_user_can( 'USER_ROLE_NAME_HERE' )
        OR current_user_can( '2ND_ROLE_NAME_HERE' )
        exit( wp_redirect( $redirect ) );
add_action( 'admin_init', 'wpse66093_no_admin_access', 100 );
share|improve this answer
so I put this at the top of my functions.php file? – Nicola Sep 24 '12 at 12:54
Either somewhere in your functions.php, or in your plugin or mu-plugins folder. As this isn't something about displaying stuff, it belongs imho in a plugin, hence the plugin header. This has the benfit that this functionality doesn't get lost, when you're changing your theme. Just change the user role names, upload it and you're done. – kaiser Sep 24 '12 at 12:59
It doesn't work. I changed the user role names to my two roles than can access it. Then I changed it to a capability e.g. Add users and I am still able to access the backend as another user role – Nicola Sep 24 '12 at 15:28
@Nicola There's a huge difference between a label in the UI (for e.g.: "Add users") and a real Role or Capability - in your case add_users. – kaiser Sep 24 '12 at 17:07
ok thanks, I tried the above code with add_users instead and it still doesn't work...if I log in as a user without that capability, I can then navigate to wp-admin – Nicola Sep 26 '12 at 11:44

The accepted answer mentions User Role but actually uses the function for User Capability

Here's the solution for User Roles

 function wpse66094_no_admin_access() {
    $redirect = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : home_url( '/' );
    global $current_user;
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    if($user_role === 'YOUR_USER_ROLE_HERE'){
        exit( wp_redirect( $redirect ) );

add_action( 'admin_init', 'wpse66094_no_admin_access', 100 );
share|improve this answer

Based on the answer provided by @kaiser (thank you btw), this is my working code, just in any case someone needs it. It is placed in functions.php file.

The condition used is, if the user can't manage_options or edit_posts.

function wpse66093_no_admin_access() {
    $redirect = home_url( '/' );
    if ( ! ( current_user_can( 'manage_options' ) || current_user_can( 'edit_posts' ) ) )
        exit( wp_redirect( $redirect ) );
add_action( 'admin_init', 'wpse66093_no_admin_access', 100 );
share|improve this answer

With @kaiser's answer I found you'll need to utilize admin_menu hook over admin_init as it fires before the !user_can_access_admin_page() check in wp-admin/includes/menu.php otherwise if the user doesn't have 'read' access to the dashboard they'll just get the 'You do not have sufficient permissions to access this page.' page rather than being redirected.

share|improve this answer

Your Answer


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.