Search code examples
phpwordpresspluginsregistrationuser-management

Prevent new WordPress users from logging in until manually "activated"?


I'm developing a plugin for WordPress which has 3 groups of users. I need to disable some users and prevent them from login. what I mean isn't preventing them to access the backend. I want to prevent them from is log in. For example, when they want to login they should see a message like this account isn't active yet. thank you guys.


Solution

  • after some search and see similar problems I solved this problam like this : first add a user meta for user status so we can checking if user is active or not then we can disable or enable users.

    add_filter( 'authenticate', 'chk_active_user',100,2);
    function chk_active_user ($user,$username) 
       {
         $user_data = $user->data;
         $user_id = $user_data->ID;
         $user_sts = get_user_meta($user_id,"user_active_status",true);
         if ($user_sts==="no")
         {
            return new WP_Error( 'disabled_account','this account is disabled');
    
          }
          else
          {
           return $user;
          }
            return $user;
         }