How to get an User Role in WordPress |

Congrats, You are Subscribed to Receive Updates.

How to get an User Role in WordPress

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;

	foreach ( $wp_roles->role_names as $role => $name ) :
		if ( current_user_can( $role ) )
			$current_usr_rle =  $role;
	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.


About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

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


Current ye@r *