Languages: English • Русский • 한국어 • (Add your language)
Retrieve the current user object (WP_User).
<?php wp_get_current_user(); ?>
For wordpress versions < 3.4: 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() always returns a WP_User object.
<?php
$current_user = wp_get_current_user();
/**
* @example Safe usage:
* $current_user = wp_get_current_user();
* if ( ! $current_user->exists() ) {
* 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
.
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: