i'm trying to create simple approval users plugin to denay or approve new user, so i used default user_status to do that, what i'm asking for how can i stop the login form to save session and add error msg if user_status = 0, something like that

if($user_status == '0'){
   //stop login sessiong
   echo $error_msg.'waiting for approval';
  //run session

is there anyway to do that


The user_status field isn't used by core, so you could use it for your own purposes. Although there may be side effects if WP ever decides to reuse it in the future.

The authenticate filter fires before the user is authenticated. You can hook into that filter and return a WP_Error object to prevent the user from logging in.

function wpse_293904_authenticate( $user, $username, $password ) {
  $user_status = get_custom_user_status_from_username( $username );
  if( ! $user_status ) {
    $error = new WP_Error();
    $error->add( 403, 'Oops. Some error message.' );
    return $error;
  return $user;
add_filter( 'authenticate', 'wpse_293904_authenticate', 20, 3 );
  • It can be better to put a priority above 10 to be called after the core authentications. And in case of denied acces, use remove_all_filters("authenticate") to remove all other authentications. – mmm Feb 12 '18 at 17:56
  • You're right that it should be at a higher priority, so I changed that. Whether to remove all other filters is a choice whether you want other plugins/themes to be able to override this particular filter. But probably you'd want to do that, yes. – Nathan Johnson Feb 12 '18 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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