Get the current logged-in user data in WordPress

Get the current user in WordPress: wp_get_current_user() and friends

Showing the name of the logged-in user, loading their own data, hiding a block from guests: almost every project needs the current user. WordPress has a small set of functions for it, and the only real pitfall is calling them too early, before WordPress knows who is logged in.

Problem: A theme should greet logged-in users by name, show members-only content, and read a custom user field.

Solution: Check is_user_logged_in(), get the WP_User object with wp_get_current_user() or just the ID with get_current_user_id(), use current_user_can() for permissions, and read user meta with get_user_meta(). Call them on init or later.

The basics

<?php
if ( is_user_logged_in() ) {
    $user = wp_get_current_user(); // WP_User

    $user->ID;
    $user->user_login;
    $user->user_email;
    $user->display_name;
    $user->roles;      // array( 'subscriber' )

    printf( '<p>%s</p>', esc_html( sprintf( __( 'Hello, %s!', 'textdomain' ), $user->display_name ) ) );
}

$user_id = get_current_user_id(); // 0 for guests

For guests wp_get_current_user() returns a WP_User with ID 0, never null, so always check the ID or is_user_logged_in() before using the data.

Permissions

<?php
if ( current_user_can( 'edit_posts' ) ) {
    // contributors and above
}

if ( current_user_can( 'edit_post', get_the_ID() ) ) {
    edit_post_link( __( 'Edit', 'textdomain' ) );
}

Check capabilities, not role names: a custom role or a plugin that adds capabilities to a role works with capability checks automatically. Why this matters in AJAX and REST handlers is explained in capability checks with current_user_can().

User meta and other users

<?php
$phone = get_user_meta( get_current_user_id(), 'ha_phone', true );

$other = get_userdata( 42 );            // any user by ID, false if not found
$by_mail = get_user_by( 'email', 'anna@example.com' );

Timing

<?php
// Too early: in the main plugin file, outside any hook, the user is not known yet
// $user = wp_get_current_user();

add_action( 'init', function () {
    $user = wp_get_current_user(); // works
} );

WordPress determines the current user from the login cookie after plugins are loaded. Calls before that (directly in a plugin file, on plugins_loaded with an early priority) may return a guest even for a logged-in user.

Caching

Pages served by a full-page cache are the same for everybody. A greeting with the user’s name in a cached page shows the name of whoever was first, or the cache plugin skips caching for logged-in users, which is the usual default. If you add user-specific output for guests (a cart count, a “recently viewed” list), load it with JavaScript from the REST API or admin-ajax.php instead of printing it into the cached HTML.

NOTE: In Multisite a user can have different roles on different sites, and current_user_can() checks the current site. To check another site, use current_user_can_for_blog( $blog_id, $capability ).