You may be looking for a WordPress user ID for many reasons. Whether you need the user ID for custom code or debugging an issue in this tutorial we’ll show you how to find user IDs in WordPress.
First we’ll cover ways to get a user ID in the WordPress dashboard and then getting a user’s ID using a PHP code snippets further down in the article.
Here are 5 ways to get a user ID in WordPress:
1. Find User ID in WordPress Dashboard
This is by far the most simple way to get a WordPress user’s ID. First, you’ll need to login to your WordPress admin dashboard.
Then head to Users>all users
From here you can select the user you want the ID of by clicking edit.
This will lead you to the user edit page. Here you’ll easily be able to see the user’s ID in the address bar of your browser.
In the example below our user’s ID is “3”.
2. See All WordPress User IDs (Using Reveal IDs Plugin)
If you want to see all user’s IDs in your WordPress dashboard you can use the Reveal IDs plugin.
After activating this plugin just head to Users>all users and you’ll see a column with the user’s ID.
This plugin is a great way to display WordPress user IDs in the admin for quick reference.
3. Get Current User ID (using PHP)
The easiest way to get the current logged in user ID with PHP is to use the get_current_user_id() function. This is the ideal solution if you’re building custom code and need the current logged in user’s ID. Read more about checking if a WordPress user is logged in with PHP.
<?php | |
echo 'The current logged in user ID is: '.get_current_user_id(); |
Alternatively if you want more than the user ID you can use wp_get_current_user(). This will return an object with all of the current logged in user’s information. For example here are ways to grab the current logged in user’s ID, username, name, display name, and email.
<?php | |
$current_user = wp_get_current_user(); | |
echo 'Username: '.$current_user->user_login; | |
echo 'User ID: '.$current_user->ID; | |
echo 'User Email: '.$current_user->user_email; | |
echo 'User First Name: '.$current_user->user_firstname; | |
echo 'User Last Name: '.$current_user->user_lastname; | |
echo 'User Display Name: '.$current_user->display_name; |
4. Get User ID by Email (using PHP)
If you’d like to get a WordPress user’s ID by an email address here is how you do that using the get_user_by() function.
<?php | |
//Get user ID by email | |
$user_data = get_user_by('email', '[email protected]'); | |
$user_id = $user_data->ID; | |
$user_email = $user_data->user_email; | |
if(!empty($user_id)){ | |
echo 'The user ID for '.$user_email.' is '.$user_id; | |
} |
5. Get User ID by Username (using PHP)
If you’re trying to get a WordPress user ID with their username it’s best to also use the get_user_by() function.
<?php | |
$user_data = get_user_by('login', 'someguy'); | |
echo 'The user ID is '.$user_data->ID; |
Getting a WordPress User ID is a simple process that can be accomplished in just a few easy steps. By following this step-by-step guide, you can quickly get a User ID and use it in any WordPress functionality you need. Good luck on your WordPress development journey! Hopefully these quick tips for getting a user’s ID in WordPress solves your issue.
If you have any questions let me know in the comments below!
5 Responses
Hi, Andy, nice post, just a question.
How can I show the user ID in my wordpress site?
I managed to create a DIV over the top nav and show it there, but I want to show it anywhere.
I mean, maybe like a H1 title at the top right of my site.
Should I ever make a DIV and style with some CSS to get the ID show?
Thanks in advance
Not fully sure of the question but you could render it in your header and style it accordingly. Additionally you could add it to a themed hook to show it up in the relevant place. It really depends on where you want it and how your theme displays things. Making a child theme and editing the header.php might be the easiest thing to do.
Nice!
Hello
I am using WP 5.7.2
I have added a custom page (custom.php) where i am trying to get userId
I am going to land on this custom page after clicking a hyperlink from home page of a site.
I have tried using wp_get_current_user() , but it looks like this function is not present
i even tried this block
‘
if ( ! function_exists( ‘wp_get_current_user’ ) ) {
return 0; //code exits from here
}
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
‘
Is the user context loaded
what am i missing?
Where is the custom.php file? in the context of your WordPress theme? You can’t use WordPress functions outside of WordPress (for example if you’re loading the custom.php file in the root of your site)