Skip to content

WP Mentals

  • Documentation
  • Demo
  • Affiliate Login

WP Mentals

  • Documentation
  • Demo
  • Affiliate Login
Wordpress Login Error

How to disable login for some user

  • by Chandan
  • 22nd July 202022nd July 2020

There are number of wordpress website allows for multi user access, it may be for user functionality in frontend or author based site to publish the post with multiple author. but some times it needs to deactivate some user for login the site, if we delete the user, we can not get the user details again because we need to deactivate for temporary based.

In this article we will discuss about the How to disable login for some user for a temporary time. Yes I would like to explain in here through two method to solve this problem.

  1. Through Custom Coding
  2. Through Plugin

1. Through Custom Coding

This method is very recommended if you know about php programming. because if we need any small functionality it is very bed idea to install a new plugin which can decrease the website performance if we use many plugins and huge data. our first mission to allow the site to lightweight performance. so let me explain how we can write a simple 3 line code to achieve our goal.

we need a hook to get the alert while a user going to login, so WordPress has an awesome filter authenticate that helps us to prevent the user login. and it gives three parameters of user object, username, and password. and if you want to prevent the login then you need to return the falsedata. if you need to get more clarity about the filter you can check the official page of the authenticate filter.

add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
     return $user;
}

Through the user object of first parameter, we can get the all user role through $user->roles so we can filter our users by roles. and if you getting the null data from $user, you can get the user by the below code, because i was getting null data in my side.

$user = (is_email($username)) ? get_user_by('email',$username) : get_user_by('user_login',$username);

Now our time to check if the user is our required role, i mean if we want to deactivate all users with role of author. And we can prevent the login for all user of author role. So our final code is,

add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
     $user = (is_email($username)) ? get_user_by('email',$username) : get_user_by('user_login',$username);
     if(in_array( 'author', $user->roles, true )){
       return new WP_Error( 'authenticate_error', __( "Sorry your account is temporary deactivate", "my_textdomain" ) );
     }else{
       return $user
     }
}

We can paste this small code to function.php file of our activated theme’s directory to prevent the login of all user of author role. so in this way we can do for any role. and if we need some user to login prevent and they aren’t in any role, we can create a user role and assign the users to this role and add this role to our code.

2. Through WordPress Plugin

Disable User Login Plugin

If you have not any knowledge about the PHP programming, do not worry you can also prevent the user’s login by a single click. But if you are a PHP programmer I must recommend you to use the first method for better performance of your site. But do not worry because i am introducing you a better lightweight plugin.

We are going to use an amazing plugin Disable User login to deactivate the user login easily with a single click.

Once installed and activated the plugin, You can get a checkbox that appears on the user profile settings in the WordPress admin panel. When you checked, the user’s account is going to be disabled and that they are going to be unable to login with the account. If they struggle to log in, they’re instantly logged out and redirected to the login page with a message that notifies them their account is disabled.

Tags:how to disable login for some userprevent wordpress loginwordpress login

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

WP Mentals | Powered by WordPress