WordPress.org

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get currentuserinfo

Description

Retrieves the information pertaining to the currently logged in user, and places it in the global variable $userdata. 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:

  • $user_login
  • $user_level
  • $user_ID
  • $user_email
  • $user_url (User's website, as entered in the user's Profile)
  • $user_pass_md5 (A md5 hash of the user password -- a type of encoding that is very nearly, if not entirely, impossible to decode, but useful for comparing input at a password prompt with the actual user password.)
  • $display_name (User's name, displayed according to the 'How to display name' User option)

Usage

 <?php get_currentuserinfo(); ?> 

Examples

Default Usage

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";
?>
Username: Zedd

User email: my@email.com
User level: 10
User first name: John
User last name: Doe
User display name: John Doe

User ID: 1

Using Separate Globals

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;
?>
Zedd's email address is: fake@email.com


: NOTE: $display_name does not appear to work in 2.5+? $user_login works fine.


<?php global $user_login , $user_email;
      get_currentuserinfo();

      echo($user_login . "'s email address is: " . $user_email;
?>

Parameters

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 } ?>
This article is marked as in need of editing. You can help Codex by editing it.