Logout all users having a specific user role in WordPress
|

Logout all users having a specific user role in WordPress

In the last tutorial, we discussed how to allow or restrict specific user roles to log into the WordPress site but what if the user is already Logged into the site? In simple words, if the user has enabled the ‘Remember me’ option while signing in to the site then even if you will change the user role he will still remain Logged in until his sessions and cookies expire.

In the previous article, we used ‘inactive_employee’ as an example of a user role for which we disabled the ability to Login for the user. Let’s add a code snippet that will Logout all the users that have ‘inactive_employee’ as the User Role.

$current_user = wp_get_current_user() ;
    $role = $current_user->roles[0] ;
    if ( $role === 'inactive_employee' ) {
//Replace 'login' with the slug of page where you want users to get redirected during Logout
        $inactiveurl = home_url( '/login/' ) ;
        wp_redirect( $inactiveurl ) ;
        wp_logout() ;
        exit ;
    }

The above code will automatically Logout all users with the role ‘inactive_employee’ if they were already logged in before the change of user role.

I hope this article has helped you to learn how to Logout all users having a specific user role in WordPress. If you have any questions, feel free to ask in the comments.

Similar Posts

Leave a Reply

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