Codex tools: Log in
Contents |
Retrieve the current user object (WP_User).
Wrapper of get_currentuserinfo() using the global variable $current_user.
<?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.
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 />';
?>
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 ( 0 == $current_user->ID ) {
// Not logged in.
} else {
// Logged in.
}
?>
Since: 2.0.3
wp_get_current_user() is located in wp-includes/pluggable.php.
Template:Pluggable Tags, get_currentuserinfo, wp_set_current_user