Languages: English • 日本語 한국어 • (Add your language)
Please see wp_get_current_user to use wp_get_current_user instead.
Retrieves the information pertaining to the currently logged in user, and places it in the global variable $current_user. Properties map directly to the wp_users table in the database (see Database Description).
Also places the individual attributes into the following separate global variables:
<?php get_currentuserinfo(); ?>
The call to get_currentuserinfo() places the current user's info into $userdata, where it can be retrieved using member variables.
<?php global $current_user; get_currentuserinfo(); echo 'Username: ' . $current_user->user_login . "\n"; echo 'User email: ' . $current_user->user_email . "\n"; echo 'User level: ' . $current_user->user_level . "\n"; echo 'User first name: ' . $current_user->user_firstname . "\n"; echo 'User last name: ' . $current_user->user_lastname . "\n"; echo 'User display name: ' . $current_user->display_name . "\n"; echo 'User ID: ' . $current_user->ID . "\n"; ?>
User email: my@email.com
User level: 10
User first name: John
User last name: Doe
User display name: John Doe
Much of the user data is placed in separate global variables, which can be accessed directly.
<?php global $display_name , $user_email; get_currentuserinfo(); echo $display_name . "'s email address is: " . $user_email; ?>
This function does not accept any parameters.
To determine if there is a user currently logged in, do this:
<?php global $user_ID; get_currentuserinfo(); if ('' == $user_ID) { //no user logged in } ?>
Here is another example:
<?php if ( $user_ID ) { ?> <!-- text that logged in users will see --> <?php } else { ?> <!-- here is a paragraph that is shown to anyone not logged in --> <p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p> <?php } ?>