get_currentuserinfo()
Sets the global variable $current_user, which contains the data of the currently logged-in user.
The data will fully correspond to the fields of the wp_users table from the database and also to the meta-fields of the current user (wp_usermeta table).
Additionally, the function populates the following global variables:
- $user_login
- $user_ID
- $user_email
- $user_identity - The username specified in the settings field 'Display as (How to display name)' (since version 3.0)
No Hooks.
Returns
true|false|WP_User. False during XML-RPC requests or when incorrect authorization cookies are present. Null when the variable $current_user is set.
Usage
get_currentuserinfo();
Examples
#1 Demo
global $current_user; get_currentuserinfo(); echo 'Username: ' . $current_user->user_login . "\n"; echo 'E-mail: ' . $current_user->user_email . "\n"; echo 'Name: ' . $current_user->user_firstname . "\n"; echo 'Last name: ' . $current_user->user_lastname . "\n"; echo 'Display as: ' . $current_user->display_name . "\n"; echo 'User ID: ' . $current_user->ID . "\n";
We get it:
Username: Zedd E-mail: [email protected] Name: John Name: Doe Display as: John Doe User ID: 1
#2 Use of individual variables.
A lot of user data is written to separate global variables, which can be used like this:
global $display_name, $user_email;
get_currentuserinfo();
echo "E-mail address of the user {$user_identity}: {$user_email}";
We get it:
E-mail address of user Leonid: [email protected]
NOTE: the $display_name variable may not work in versions above 2.5, use $user_identity instead.
#3 Check if the user exists.
To check if the user is authorized (logged in), use the following conditional tag:
if( is_user_logged_in() ){
// authorized user
} #4 Another example: how to split the display between authorized and unauthorized
How to show one text to authorized users and another to unlogged users:
<?php if ( is_user_logged_in() ) { ?>
<!-- text that will be seen by authorized users -->
<?php } else { ?>
<!-- this text will be seen by users who are not logged in. -->
<p>To see the entire <a href="<?php bloginfo('url'); ?>/wp-register.php">register</a>.</p>
<?php } ?>
Notes
Changelog
| Since 0.71 | Introduced. |
| Deprecated since 4.5.0 | Use wp_get_current_user() |
get_currentuserinfo() get currentuserinfo code WP 6.9.1
function get_currentuserinfo() {
_deprecated_function( __FUNCTION__, '4.5.0', 'wp_get_current_user()' );
return _wp_get_current_user();
}