Codex

Function Reference/wp get current user

Contents

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

E.g.: To determine if there is a user currently logged in, do this: <?php
$current_user 
wp_get_current_user();
if ( 
== $current_user->ID ) {
    
// Not logged in.
} else {
    
// Logged in.
}
?>

Default Usage

The call to wp_get_current_user() return WP_User object. <?php
    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 />';
?>

Change Log

Since: 2.0.3

Source File

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

Related

Template:Pluggable Tags, get_currentuserinfo, wp_set_current_user

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