Function Reference/get currentuserinfo

来自WordPress中文文档
Williamsba1讨论2010年6月14日 (一) 15:21的版本 (removed text about function not working in 2.9.2. Function works fine in 2.9.2 and 3.0 RC3)
跳转至: 导航搜索

Description

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

  • tt$user_login/tt
  • tt$user_level/tt
  • tt$user_ID/tt
  • tt$user_email/tt
  • tt$user_url/tt (User's website, as entered in the user's Profile)
  • tt$user_pass/tt (A hash of the user password -- useful for comparing input at a password prompt with the actual user password.)
  • tt$display_name/tt (User's name, displayed according to the 'How to display name' User option)

Usage

%%% ?php get_currentuserinfo(); ? %%%

Examples

Default Usage

The call to ttget_currentuserinfo()/tt places the current user's info into tt$current_user/tt, 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;
?

div style=border:1px solid blue; width:70%; padding:10pxUsername: Zeddbr / User email: my@email.combr / User level: 10br / User first name: Johnbr / User last name: Doebr / User display name: John Doebr / User ID: 1/div

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;
?

div style=border:1px solid blue; width:50%; padding:10pxZedd's email address is: fake@email.com/div


: 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 (#0039;#0039; == $user_ID) {
         //no user logged in
      }
?

Here is another example:

pre ?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 --

pBy a href=?php bloginfo('url'); ?/wp-register.phpregistering/a, you can save your favorite posts for future reference./p

?php } ? /pre

includeonlydiv style=clear:both; background-color:#EEEEFF; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This article is marked as in need of editing. You can help Codex by editing it./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude