How to Get User ID in WordPress? (Quick Ways)

Not sure how to get a user ID in WordPress? In WordPress, there are many reasons when we need to find the user ID of an account and sometimes we are not quite sure how to get it. Sometimes even when I develop custom WordPress elements, I have to google the way of finding the user ID of a user to perform certain activities.

But not now, here in this article, I’m going to show you a few easy ways to get the user ID of a WordPress user and perform certain actions using it.

There are several methods to find the user ID, including;

  • Finding User ID from WordPress Dashboard
  • Finding User ID Using Plugin
  • Printing Current User ID (Custom Code)
  • Getting User ID from User’s Email (Custom Code)
  • Getting the List of Users (with Metadata)

In the first two steps, we get the user ID directly from the WordPress dashboard and by using a plugin, and in the third, fourth, and fifth steps, we provide custom PHP code to the developer for printing or doing activities based on the user ID.

So without wasting more time, let’s get started…

Top

Why Get User ID in WordPress?

On this blog, I’ve covered a lot of stuff related to custom elements and the benefits of using custom elements in WordPress. Adding custom elements to a website will allow it to perform certain functions/actions.

Whenever we are creating a custom element requiring a User ID, we have to obtain it using two different methods. The first one is to grab a specific user ID from WordPress and perform actions based on it, while the second grabs the user ID dynamically and performs actions based on it.

If, for example, we wanted to create an author page on our website using custom elements, first we’d need to retrieve the user ID dynamically, and then show the details about that user.

Due to this, I have covered both methods in this guide, so that you can choose the method that works best for you so that you don’t have to search around for answers.

Top

How to Get User ID in WordPress

The process for finding the user ID in WordPress is easy, and you can easily retrieve the ID even if you’re not a technical person by using the five methods I’m sharing below.

1. Finding User ID from WordPress Dashboard

This is the very first method to retrieve the user ID of any user registered on your website. 

In this method, you’ve to log in to your admin console and then click on the “Users > All Users” option showing on the left side of your menu bar. 

get user
Click on “Edit” Showing Below User’s Name

Hovering over the user’s name will show you a few options where you will also have an option to “edit” the user’s profile. Simply click on this option.

On clicking this, a new page will appear with a URL with some custom parameters like “https://thewpx.com/wp-admin/user-edit.php?user_id=4&wp_http_referer=?“…

get user in wordpress, wordpress get user by id, wordpress get_user_by, wordpress get_users
Find the Parameters in the URL

If we further beautify this URL, you’ll clearly see a value named “user_id=4” after the “https://thewpx.com/wp-admin/user-edit.php?” value. 

The value “4” represents the user ID.

This method does not provide you with the user ID of the currently logged-in user. It will work only for obtaining the IDs of other users on your website, not your own.

2. Finding User ID Using Plugin

There are some cases where we need the user ID of a few people and finding the user ID from the first method gets too boring and time-consuming for us.

In this case, we can use a plugin named “Reveal IDs” which is available for free on the WordPress repository.

You can simply install this plugin on your website by logging into your admin console and after that click on the “Plugins > Add New Plugin“.

Here you’ve to search for the “Reveal IDs” and then simply click on the “Install” and then “Activate” button. 

get user in wordpress, wordpress get user by id, wordpress get_user_by, wordpress get_users
Retrieving User ID using “Reveal IDs” Plugin

Once the plugin is installed on your website, simply visit the admin console and then click on the “Users > All Users” and it will show the user ID of all users you’ve on your website.

Aside from finding the user ID, this plugin also has the ability to show the IDs of posts, pages, attachments, and other post types available on your website. 

3. Printing Current User ID (using PHP)

In the above two steps, I’ve shared the methods to grab the user ID manually but what happens when we want to get the user ID of currently logged-in users on our website and perform actions accordingly? What to do in this case?

So in this case, we’ve to use a pre-made function named “get_current_user_id()” to retrieve the user ID of currently logged-in user.

Here’s the example code to retrieve the user ID 

The code will pass the user ID of anyone logged in to your website. After logging in, for example, this code will output the value “1” which corresponds to my ID.

4. Getting User ID by User’s Email (using PHP)

There are times when we want to grab the user ID available on our website by entering the email ID they’ve registered on our website. 

In these cases, we can use another predefined function named “get_user_by()” and passing the email ID along with it. 

Here’s the code for this;

The only thing that you need to do is replace “[email protected]” in the code I shared above with the email address for the user for whom you want the ID.

5. Getting the List of Users (with Metadata)

This is the last method which we’re covering in this guide. 

In this method, I’ll share the custom code which helps in getting the list of all users registered on our website along with their metadata using for loop.

If you also want to get the list of users on your website with their metadata, then use this code;

This code can be helpful in many cases, and I usually prefer using this code to create custom elements where I list every user along with their details and the number of posts they’ve published on the website.

Top

Frequently Asked Questions 

Still, have doubts? We all get confused sometimes, and there are times when we have several questions in our minds. So here are some questions we usually get asked by our readers.

I hope these FAQs help you to answer all your questions.

Q. Why Get User ID in WordPress?

In some cases, we may have to create dynamic user ID pages that display the author’s names, along with other details, as well as a few articles that they have recently published and we can retrieve user ID to perform these activities.

Furthermore, we also need a user ID to avoid blocking or limiting access to our website for unauthorized users, and retrieving user IDs can help here.

Q. What Type of Actions We Can Perform Using User ID?

There are various types of actions that you can perform after getting the user ID, and some of these are;

  • Sorting articles based on user ID,
  • Creating an author profile for every user,
  • Blocking and unblocking specific actions on the website, 
  • Setting specific permissions based on user ID.

…and a lot more.

Q. Is it Safe to Write Custom Code in WordPress?

Yes, it’s safe to write custom code in WordPress to perform specific actions. Generally, you can edit the “functions.php” file within your theme folder. However, while updating the theme, the coding will revert back to its original state.

Alternatively, you can use a plugin named “Code Snippets“, which I recommend, in order to write and edit custom codes within your WordPress website but before performing any of these activities, I’ll recommend you to take a backup of your website.

Wrapping Up!

I hope this article helped you to get the user ID of WordPress users. I have tried to put all the methods for finding the user ID in WordPress that I have used while developing custom themes or plugins.

If you have any questions or thoughts, feel free to drop them in the comment section. I answer every single question of yours, as soon as possible.

Thank you so much for reading this article!

TheWPX is a blog where we cover topics related to Blogging, WordPress, SEO, and Affiliate Marketing.
All the information which is published on our website is collected through independent research and tests. The data is being verified through consultation with reliable sources.
Also, We constantly update the content on our website, keeping it fresh and relevant to our readers at all times.

Leave a Comment

Sunny Kumar

Sunny Kumar, Founder of TheWPX

Hi, I'm Sunny Kumar, a passionate tech enthusiast and blogger from New Delhi, India. I usually writes about PC building, WordPress, and SEO on my blogs. And I hold certifications in SEO, Cloud Computing, Telecom & Networking, and CEH, along with an IT degree from IIT-D.