How to get an User Role in WordPress
- Article
- Comment
How to get an User Role in WordPress. getting an user role have few methods. Let’s check the function here. For current user you can use the below function to get the current user role.
function kv_get_current_user_role(){ $current_usr_rle = ''; global $current_user, $wp_roles, $wpdb; get_currentuserinfo(); foreach ( $wp_roles->role_names as $role => $name ) : if ( current_user_can( $role ) ) $current_usr_rle = $role; endforeach; return $current_usr_rle; }
And the Above code looks big enough to work, we will use a simpler code which is more simple and runs within the WordPress function itself.
$user = new WP_User( $user_id ); $role= wp_sprintf_l( '%l', $user->roles ); echo $role;
You can use change the `$user_id` to current user ID to get current user role with in it.