1

I have a site where customers can register themselves (they get a user role "pending"). However, what i want is that after this registering these user role cannot automatically log in or log out directly. They must also not be able to log in. Login must therefore be denied for these user role.

2
  • I don't think that's what I need. I have the user roles already set. Only for the user role "pending" I want this when a new account is getting created (by a front page form) not logged in automatically or logged out direct, and also he can not log in. I as admin change this new account "pending" to "customer". With the user role "customer", the customer now can login and look at the pages that I have assigned to this user role.
    – Arnolddew
    Commented Apr 16, 2017 at 15:47

1 Answer 1

2

What you want to do is hook into the authenticate hook, check if the user has the pending role, and if so, throw an error.

//* Add filter to the authenticate hook
add_filter( 'authenticate', 'wpse_263762_authenticate', 20, 3 );
function wpse_263762_authenticate( $user, $username, $password ) {
  //* Check if the user has the pending role
  if( ! is_wp_error( $user ) && in_array( 'pending', $user->roles ) ) {
    //* Throw an error
    $error = new WP_Error();
    $errorMessage = __( 'Your error message goes here.' );
    $error->add( 401, $errorMessage );
    return $error;
  }
  //* Or return the user
  return $user;
}
3
  • Thanks for your answer. Only i tested it, and it looks the code don't work. I also get the error now on the wp-admin login page: Warning: in_array() expects parameter 2 to be array, null given in www.mysite.com on line 201
    – Arnolddew
    Commented Apr 16, 2017 at 15:57
  • If $user is a WP_User object, then it should have a roles property that is an array (since WP 2.0.0). I've added a check to make sure that $user isn't a WP_Error object. Commented Apr 16, 2017 at 16:09
  • It looks like this is it! My cup of soup. Thanks Nathan!!!
    – Arnolddew
    Commented Apr 16, 2017 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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