How to Get a WordPress User ID (PHP + Dashboard Methods)

The fastest way to get a WordPress user’s ID is to open their profile in the admin (Users → All Users → Edit) and read it straight from the URL bar. For code, use get_current_user_id() inside any PHP context to grab the current logged-in user’s ID:

$user_id = get_current_user_id();
// Returns the integer ID, or 0 if no user is logged in

Below I’ll cover five ways to get a user ID: two from the WordPress dashboard and three in PHP, plus how to go in the reverse direction (get a user object from an ID), common gotchas like AJAX contexts where get_current_user_id() returns 0, and a troubleshooting FAQ.


5 ways to get a user ID in WordPress:

1. Find a user ID from the WordPress dashboard

The quickest no-code method. Log into WP admin and go to Users → All Users.

All Users option in the WordPress admin dashboard
All Users in the WordPress dashboard

Hover over any user’s row (don’t click) and look at your browser’s status bar. The edit link looks like:

https://yoursite.com/wp-admin/user-edit.php?user_id=3

The number after user_id= is the ID. If you’d rather click through, choose Edit on any user and the same ID shows in the URL bar of the edit page.

Edit link on a WordPress user in the admin users list
Click Edit on a WordPress user
WordPress user ID visible in the browser address bar
User ID visible in the URL bar (here: 3)

2. See all user IDs at once (Reveal IDs plugin)

The dashboard doesn’t show the ID column by default. If you want to see every user’s ID in the users list without clicking into each profile, install the Reveal IDs plugin.

After activating, go to Users → All Users and a new ID column appears. The plugin also adds ID columns to posts, pages, categories, and tags, so it’s handy well beyond just users.

Reveal IDs plugin adding an ID column to the WordPress users list

3. Get the current logged-in user ID with PHP

Use get_current_user_id() anywhere you need the ID of the user who’s currently logged in:

echo 'The current logged in user ID is: '.get_current_user_id();

It returns an integer, or 0 if no user is logged in. Guard against that before using the result, especially if the code runs on the front-end where visitors may not be logged in:

if ( is_user_logged_in() ) {
    $user_id = get_current_user_id();
    // safe to use $user_id here
} else {
    // not logged in; handle accordingly
}

If you need more than just the ID (name, email, login, role), use wp_get_current_user(). It returns the full WP_User object:

$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;

For more on checking login state before running user-specific code, see how to check if a WordPress user is logged in.


4. Get a user ID by email address (PHP)

When you have an email address and need the matching user ID, use get_user_by() with the 'email' field:

// 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;
}

get_user_by() returns false if no user matches. Always check the result before using ->ID, because calling a property on false throws an error in modern PHP.


5. Get a user ID by username (PHP)

Same function, different field. Pass 'login' (for the username) or 'slug' (for the URL-friendly version of the username, which is what shows up in author URLs):

$user_data = get_user_by( 'login', 'someguy' );
echo 'The user ID is '.$user_data->ID;

get_user_by() accepts four lookup fields in total:

  • 'id' – look up by user ID (the reverse direction)
  • 'login' – username
  • 'email' – email address
  • 'slug' – user nicename (URL-safe username)

Reverse direction: get a user object from an ID

When you already have an ID and need the full user object, get_user_by( 'id', $user_id ) is the cleanest call:

$user = get_user_by( 'id', 42 );

if ( $user ) {
    echo $user->display_name;
    echo $user->user_email;
}

Alternative: get_userdata( $user_id ) does the same thing and is slightly more readable in some contexts. Both return a WP_User object or false.

$user = get_userdata( 42 );

if ( $user ) {
    echo $user->display_name;
}

Frequently asked questions

How do I get the current user ID in WordPress?

Call get_current_user_id(). It returns the ID of the currently logged-in user, or 0 if no user is logged in. Wrap it in is_user_logged_in() before using the ID on the front-end, since visitors aren’t always logged in.

How do I get a user by ID in WordPress?

Use get_user_by( 'id', $user_id ) or get_userdata( $user_id ). Both return a WP_User object with every profile field (ID, user_login, user_email, display_name, roles, capabilities) or false if no user matches.

Why is get_current_user_id() returning 0?

Two common causes: the code is running before WordPress has finished loading the current user (hook into init or later), or the context has no logged-in user (public page view, cron job, server-side REST calls without authentication). Check with is_user_logged_in() first, and if you need the user in an AJAX handler, make sure the AJAX call includes nonce authentication.

How do I get a user ID by email in WordPress?

Use get_user_by( 'email', '[email protected]' ) to get the WP_User object, then access the ->ID property. Always check the return value for false before using it, since calling a property on false causes a fatal error on modern PHP versions.

How do I get a user ID by username?

Use get_user_by( 'login', 'username' ). Returns the WP_User object whose user_login matches the string, or false. If you have a URL-safe slug (the version that appears in author URLs), use 'slug' as the field instead.

What is the difference between get_current_user_id() and wp_get_current_user()?

get_current_user_id() returns an integer (just the ID). wp_get_current_user() returns the full WP_User object with every field available. Use the first when you only need the ID; use the second when you need the email, display name, roles, or user meta in the same call.

How do I get user meta by user ID?

Use get_user_meta( $user_id, $meta_key, true ). The third argument (true) returns a single value; use false or omit it to return an array of all values for that key. For example: $nickname = get_user_meta( 42, 'nickname', true );.


Bottom line

In the admin, hover over a user or open their edit page and read the ID from the URL bar. In code, get_current_user_id() for the current logged-in user, get_user_by( 'email'|'login'|'slug', $value ) to go from any identifier to a user object, and get_userdata( $id ) for the reverse (ID → object). Always guard PHP usage with is_user_logged_in() or check for false return values.

Related: how to get a post ID, check if a user is logged in, WordPress user roles explained, or browse the full WordPress code snippets library.

Picture of Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 15 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way. Found this article helpful? Buy Me A Coffee

5 Responses

  1. 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

    1. 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.

  2. 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?

    1. 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)

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Tips Monthly
Get the latest from SmartWP to your inbox.