WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp get current user

Description

Retrieve the current user object (WP_User).
Wrapper of get_currentuserinfo() using the global variable $current_user.

Usage

<?php wp_get_current_user(); ?> Use the init or any subsequent action to call this function. Calling it outside of an action can lead to troubles. See #14024 for details.

Parameters

none
(none) (none) This function does not accept any parameters.
Default: none

Return Values

WP_User (object) 
WP_User object where it can be retrieved using member variables.

Examples

Default Usage

The call to wp_get_current_user() return WP_User object.
<?php
    $current_user 
wp_get_current_user();
    
/**
     * @example Safe usage: $current_user = wp_get_current_user();
     * if ( !($current_user instanceof WP_User) )
     *     return;
     */
    
echo 'Username: ' $current_user->user_login '<br />';
    echo 
'User email: ' $current_user->user_email '<br />';
    echo 
'User first name: ' $current_user->user_firstname '<br />';
    echo 
'User last name: ' $current_user->user_lastname '<br />';
    echo 
'User display name: ' $current_user->display_name '<br />';
    echo 
'User ID: ' $current_user->ID '<br />';
?>

Checking Other User Attributes

This example demonstrates how to manually determine if a user is logged in.

IMPORTANT NOTE: This is for demonstration purposes ONLY. The correct way to determine whether a user is logged in is to use the function is_user_logged_in().
<?php
$current_user 
wp_get_current_user();
if ( 
== $current_user->ID ) {
    
// Not logged in.
} else {
    
// Logged in.
}
?>

Change Log

Since: 2.0.3

Source File

wp_get_current_user() is located in wp-includes/pluggable.php.

Related

Current User Functions: get_current_user_id(), get_currentuserinfo(), wp_get_current_user(), wp_set_current_user(), current_user_can(), current_user_can_for_blog()

Get User Functions:

See also index of Function Reference and index of Template Tags.