visit EYEWP.COM now!! https://eyewp.com/
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(); } }
Once saved, visit your website and check the returned role values of the current user.
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.
==========
Premium plugins, extensions, and themes for WordPress and WooCommerce – Download Free
Don’t be late. visit EYEWP.COM now!! https://eyewp.com/
Grow your business with thousands of free and paid plugins, extensions, and themes for WordPress and WooCommerce across all your logistical, technical, and marketing needs.
Platform
WordPress – Shopify – Prestashop – Magento – Joomla – Opencart
Brands
WooCommerce – Yoast – Gravity Forms – YIThemes – CodeCanyon – Themeforest – Easy Digital Downloads – WP Rocket – iThemes – AffiliateWP – Give – Toolset / WP-Types – WP All Import – Advanced Custom Fields – Design N Code – WC Vendors Pro – The Events Calendar – Ultimate Member – ArrayThemes – Duplicator Pro – FacetWP – Elementor Pro – SearchWP – Admin Menu Editor – Automatewoo – MainWP – WPFomify – Restrict Content Pro – WPFomify – MyThemeShop – Elegant Themes – WPMU Dev – Thrive Themes – WP Job Manager – Beaver Builder – Divi Booster – Download Monitor – WPML – Themify – Gravity Perks – Admin Columns Pro – LearnDash – Studiopress – EventON – CSS Igniter – Gravity Flow – AppThemes – CyberChimps – Elementorism by CSSIgniter – OceanWP – WP Rich Snippets – Rank Math – Crocoblock – polylang – UpdraftPlus – GeneratePress – EngineThemes – Iconic – clearfy – formidableforms – Wordfence – restrict content pro – premmerce – LifterLMS – ProteusThemes – JoomUnited – wpmailsmtp – aioseo
Categories
WordPress Plugins – WordPress Themes – WordPress Templates – WordPress Builder – WooCommerce Plugins – WooCommerce Extensions – WooCommerce Themes – CMS Templates – HTML Templates – Web Templates – SEO Plugins
Don’t be late. visit EYEWP.COM now!! https://eyewp.com/
Get 5500++ Premium Plugins, Extensions, and Themes
Access them all and receive automatic updates as soon as they are available.
Are you looking for the best plugins for your WP website? WordPress Plugins and Themes Download premium and themes. Unlimited Domain Usage. Save Thousands Of Dollars. Instant Download. Latest Version. Styles: Unlimited Domain Usage., Free New Version.
5000+ WP Plugins · 5000+ WP Themes · 5500+ E-Commerce Plugins