get_currentuserinfo()
Deprecated from version 4.5.0. It is no longer supported and can be removed in future releases. Use wp_get_current_user() instead.
Populate global variables with information about the currently logged in user.
1 time — 0.000013 sec (very fast) | 50000 times — 0.01 sec (speed of light) | PHP 7.0.5, WP 4.5.1
No Hooks.
Return
true|false|WP_User
. False on XMLRPC Request and invalid auth cookie, WP_User instance otherwise.
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.7.1
function get_currentuserinfo() { _deprecated_function( __FUNCTION__, '4.5.0', 'wp_get_current_user()' ); return _wp_get_current_user(); }