Showing posts with label user role. Show all posts
Showing posts with label user role. Show all posts

Monday, 1 July 2013

Block user role login

We can block the user login by the Role. This will work before the user name and password validation. So we can block the user before the validation for this we need to assign the user to particular role in this I used the "blocked" role which is custom one you can make any role to get blocked.


Sample code to block the user by role.Just paste the following code in either plugin files or in the functions.php ( theme file )


 add_filter('wp_authenticate_user', 'custom_block_user_role_login', 10, 2);
if(!function_exists('custom_block_user_role_login')) {
    function custom_block_user_role_login($user, $password) {
       
        $role = $user->wp_capabilities;


/*
* In below code "blocked" is the user role you can assign it to another role 
* which you want to get blocked
*/
        if(array_key_exists('blocked', $role)) {
            return new WP_Error('block_user', __('You are Blocked.'));
        }
        return $user;
    }
}

Monday, 13 May 2013

Get current user role

Its easy to get the currently logged-in user role by using the global variable $current_user. Following function can be used to get the current user's role.

if(!function_exists('custom_get_current_user_role')) {
    function custom_get_current_user_role(){
         if(is_user_logged_in()) {
            global $current_user;
            $user_role = $current_user->roles[0];
            return $user_role;
        }
        return FALSE;
    }
}