4

I do not want the users (role: subscriber) to be able to login via the wp-admin.

How would you suggest I do this ?

14

You can remove the Read capability. That will prevent all access to wp-admin.

function remove_read_wpse_93843(){  
    $role = get_role( 'subscriber' );
    $role->remove_cap( 'read' );    
}
add_action( 'admin_init', 'remove_read_wpse_93843' );

See the note in the Codex about only needing to run this once.

Then you need to hide the admin bar.

function hide_admin_wpse_93843() {
  if (current_user_can('subscriber')) {
    add_filter('show_admin_bar','__return_false');
  }
}
add_action('wp_head','hide_admin_wpse_93843');

And probably want to redirect subscriber logins:

function redirect_sub_to_home_wpse_93843( $redirect_to, $request, $user ) {
    if ( isset($user->roles) && is_array( $user->roles ) ) {
      if ( in_array( 'subscriber', $user->roles ) ) {
          return home_url( );
      }   
    }
    return $redirect_to;
}
add_filter( 'login_redirect', 'redirect_sub_to_home_wpse_93843', 10, 3 );

Other attempts to access wp-admin will result in the "You do not have sufficient permissions..." almost-white screen.

That is somewhat bare-bones but I think it gets you there.

3
  • Thanks, just what I was looking for. Shall give it a shot now. Mar 29 '13 at 21:22
  • Tanks for your answer. It is just what I was looking for. Jul 13 '14 at 23:15
  • 1
    remove_cap() works different nowadays. Refactor your code to one of the examples on this page.
    – luukvhoudt
    Mar 22 '18 at 8:44

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