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 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

1 Answer 1

up vote 5 down vote accepted

Plugin

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.

<?php
! 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
1  
@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

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.