Search for a plugin or theme

How to Get Current User Role and Name in WordPress

You are currently viewing How to Get Current User Role and Name in WordPress

When adding a custom code or creating a plugin in WordPress, you may need to use the information of the specific current user.

Getting the current user values such as role or ID will enable you to segment and display unique content to different users.

In this article, you’ll learn how to get the current user role and ID in WordPress.

Why Get Current User Data in WordPress

There are many reasons why to get the current user data in WordPress:

  • Display different content to each user based on his role. For example, an active customer can view content that others can’t view.
  • Add a login and logout link to the header based on the user’s status.
  • Special discounts for logged-in users.
  • Remove the admin bar for all non-admin users.
  • Congrats to logged-in users by their first names on their account page.

These were just a few examples of the unlimited possibilities you can achieve when working with the WordPress user’s object and getting data such as role, ID, and name.

In one of our blog posts, we used a function to get the current user role to create a registration form with Elementor.

You can see many more examples when getting the current user object comes in handy in some of the most common code snippets for WordPress.

Some of the most often uses of the get current user object in WordPress will require us to retrieve the user role, ID, and name, and in the following few sections, we will learn exactly how to do it.

Get Current User Role in WordPress

Getting the current user role in WordPress is a two steps process. We will first need to check if the visitor is logged in, and if so, what’s its role.

To check if the visitor is logged in and an actual user, use this function:

is_user_logged_in()

Once we verify that the visitor is logged in, we can use the wp_get_current_user() function and get the user’s object along with all the data that comes with it.

WordPress user’s object includes relevant and valuable information regarding the logged-in user. The -> is used in object scope to access methods and properties of an object. Knowing that, wp_get_current_user()->roles will get us the current user role.

The complete code will look like that:

 if( is_user_logged_in() ) {
 $user = wp_get_current_user();
 $roles = ( array ) $user->roles;
 return $roles; // This returns an array
 // Use this to return a single value
 } else {
 return array();
 }

You can test the function by passing it inside the functions.php file of your child theme, hook it to the header, and print the result:

add_action( 'wp_head', 'pfwp_get_current_user_role'); 

function pfwp_get_current_user_role() {
	
    if( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
        print_r($roles); // This will print the array of the current user roles
    } else {
    return array();
    }

}
Get current user role with a function

Once saved, visit your website and check the returned role values of the current user.

Array current user role

If the user has multiple roles, such as admin and subscriber, you can choose a specific role from that array. Replace print_r($roles) with the desired value of the array, for example print_r($roles[0]).

Once you modify the code, please remember to save the changes and verify that it’s working as expected.

Get Current User ID in WordPress

Getting the current user ID in WordPress is a simple process because there is no need to invoke a new WP_User_object as we did above when getting the user role.

The code below is very similar but with less overhead. It will retrieve the current user’s ID, or 0 if no user is logged in.

get_current_user_id()

To display the user ID or congrats to a visitor with no ID, use a function like so:

if ( is_user_logged_in() ) {
echo 'User ID: ' . get_current_user_id();
} else {
echo 'Hello visitor!';
}

You can trigger it to the website header or any other part of your website using WordPress hooks.

Get Current User Name in WordPress

As explained above, the wp_get_current_user() function returns the WP_User object.

The WP user object is full of data regarding the current user.

Some of the attributes that we can scope from that object are username, name, display name, etc.

Here are some examples of how to get that data:

Get current user first name:

$current_user->user_firstname

You can use it in a function to display the username first name like so:

if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
echo 'Hello: ' . $current_user->user_firstname;
} else {
echo 'Hello visitor!';
}

Get current user last name:

$current_user->user_lastname

Get current user display name:

$current_user->display_name

Those were only some examples of the detailed content that you can use with the user object.

Regardless of the function, I always recommend using a backup plugin before modifying core files if something goes wrong.

Conclusion

In this article, you learned how to get the current user data such as role and ID in WordPress.

Leave us a comment and tell us the functions and data you used or if you have any further questions regarding the process.

Also, make sure to subscribe to our YouTube channel and like our page on Facebook.

PluginsForWP

PluginsForWP is a website specializing in redistributing WordPress plugins and themes with a variety of knowledge about WordPress, internet marketing, and blogging.

Leave a Reply


Get notified about new content and coupon codes!

JOIN OUR COMMUNITY OF 120,000+ WORDPRESS ENTHUSIASTS