Unlike humans, who prefer to go by names, organized systems and databases utilize numbers. After all, if two people, for instance, have the same first and last name, and there are no unique features to distinguish them, the system cannot determine to whom the action applies. Additionally, written information is inefficient because the data it occupies adds up as the number of rows and columns grows. That’s why you see systems assign a unique number to each user. That way, it will always find an entry and a correct one at that. That’s our topic for the day – learning how to get current user ID in WordPress
1. Get the current user ID from the WordPress URL
Although the methods below are basic and leave little room for error, this one is dead simple. It uses two things you must be familiar with by now: your Internet browser and the WordPress Admin Dashboard. Here’s how that works:
- Log in to your WordPress Admin Section.
- Click on Users in the left sidebar.
- Select All users.
- Find the user you seek on the list.
- Click on Edit below their name.
- Check the URL of the page that opened. Find the ID under:
your-website.com/wp-admin/user-edit.php?user_id=NUMBER&wp_http_....
2. Find current WordPress user ID using a function
We’ll begin with the one WordPress Code Reference mentions. It is based on a function get_current_user_id(). Although you can read about the get_currentuserinfo() function, disregard it since it’s deprecated. With that said, the function will show you user’s ID if they are logged in, and in its most basic form looks like this:
$int = get_current_user_id();
Practical application
Here are two rudimentary practical examples:
1. A simple option
The simplest function to display your ID:
$current_user_id = get_current_user_id();
echo 'Your User ID is: ' .$current_user_id;
Many people correctly noticed there are ways to reduce overhead in the second practical example without losing functionality. Here’s how an optimized function can look:
if ( is_user_logged_in() ) {
echo 'User ID: ' . get_current_user_id();
} else {
echo 'Welcome to our website!';
}
2. Option found in WordPress
A frequently used model is in a portion of the /wp-includes/user.php file located in the root of your website folder after you access it via FTP. Here’s that tidbit so you don’t have to seek it out manually:
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) ) {
return 0;
}
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}
Both this one and the ones above return ID if the user is logged in, and 0 if they’re unavailable. The user won’t be assigned any permissions if it’s the latter. The function also doesn’t accept parameters.
The key difference
The two functions are identical in purpose and will work equally well. However, the latter, wp_get_current_user, also allows for fetching information such as:
- Username: $current_user->user_login
- E-mail address: $current_user->user_email
- First name: $current_user->first_name
- Last name: $current_user->last_name
- Display name: $current_user->display_name
Some common use cases
Website owners oftentimes need to show which role a user has on the website based on their ID, but not necessarily show the ID itself. Here’s a simple example of that:
$user = new WP_User(get_current_user_id());
echo $user->roles[0];
Another frequent example is configuring exclusive content or functionality for a user that has a specific ID. If that’s the case, you merely have to start with:
$current_user = get_current_user_id();
if( $current_user == 1 ){ // replace this with yours
// Add code for doing/showing something here
}
3. Discover an ID based on username and the other way around
If you know the user’s login name (not display name), you can easily find their ID. After that’s done, you can reverse the process. Do this to get the current user ID based on login name:
$the_user = get_user_by('login', '[username without brackets]');
$the_user_id = $the_user->ID;
To do the opposite, you merely must use the get_userdata() function. This is one of two options you can use:
$the_user = get_user_by( 'id', 28 ); // replace 28 with your number
echo $the_user->user_login;
The second option merely uses a different function:
$the_user = get_userdata( 28 );
echo $the_user->user_login;
4. Finding current user ID using e-mail and vice versa
You can use the methods mentioned above for e-mails but must change user_login to user_email. To allow you to quickly copy it into your code and make changes, we’ll quickly go over it again. The explanations above still stand.
Learn ID based on e-mail address this way:
$the_user = get_user_by('email', 'username@mail-provider.com');
$the_user_id = $the_user->ID;
Find an e-mail address based on the user ID in this fashion:
$the_user = get_user_by( 'id', 28 );
echo $the_user->user_email;
$the_user = get_userdata( 28 );
echo $the_user->user_email;
5. Print a list of current user IDs based on full name
To clarify, you can either search by first or last name, and the result will be a printed list of IDs for all users that correspond to that first or last name. We’ll only demonstrate the former case:
global $wpdb;
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'Peter'" );
if( $users ) {
foreach ( $users as $user ) {
echo '<p>' . $user->user_id . '</p>';
}
} else {
echo 'There are no users with that name.';
}
If you want to search by the last name, merely change first_name (only mentioned once, you can’t miss it) to last_name, and that’s it.
6. Using post ID to fetch the ID of a user
Getting page ID in WordPress is something we discussed previously. Well, finding post IDs is nearly identical. Even if you aren’t already familiar, a quick glance tells you all you must know. In this case, if we know the author of a post, we can quickly find their ID using a WP_Post object. Add the following code to get the current user ID based on a WordPress post:
$my_post = get_post( $id ); // $id - This is post ID
echo $my_post->post_author; // This will print the ID of the post author
7. Get current user ID from a custom column in WordPress
Finally, if you’d like to have all IDs in one place, you can utilize the Users page in WordPress from method 1. You simply must customize it to show extra information. This method will simply add a column titled “ID” with users sorted from lowest to highest and keep IDs visible at all times. While you can do this manually, installing a WordPress plugin does the job faster, more efficiently, persists through updates, and carries very little risk. The choice is yours, but a well-known free option available in the repository since WordPress 3.0 is Reveal IDs by Oliver Schlöbe.