“Function Reference/get currentuserinfo”的版本间的差异

来自WordPress中文文档
跳转至: 导航搜索
(remove new page created)
 
(1个版本)
 
(未显示6个用户的16个中间版本)
第1行: 第1行:
 
== Description ==
 
== Description ==
  
Retrieves the information pertaining to the currently logged in user, and places it in the global variable <tt>$userdata</tt>. Properties map directly to the wp_users table in the database (see [[Database Description#Table:_wp_users|Database 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#Table:_wp_users|Database Description]]).
  
 
Also places the individual attributes into the following separate global variables:
 
Also places the individual attributes into the following separate global variables:
 
:* <tt>$user_login</tt>
 
:* <tt>$user_login</tt>
:* <tt>$user_level</tt>
 
 
:* <tt>$user_ID</tt>
 
:* <tt>$user_ID</tt>
 
:* <tt>$user_email</tt>
 
:* <tt>$user_email</tt>
 
:* <tt>$user_url</tt> (User's website, as entered in the user's Profile)
 
:* <tt>$user_url</tt> (User's website, as entered in the user's Profile)
:* <tt>$user_pass_md5</tt> (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.)
+
:* <tt>$user_pass</tt> (The phpass hash of the user password - useful for comparing input at a password prompt with the actual user password.)
:* <tt>$user_identity</tt> (User's name, displayed according to the 'How to display name' User option)
+
:* <tt>$display_name</tt> (User's name, displayed according to the 'How to display name' User option)
 +
:* <tt>$user_identity</tt> (User's name, displayed according to the 'How to display name' User option (since 3.0))
  
 
== Usage ==
 
== Usage ==
第20行: 第20行:
 
=== Default Usage ===
 
=== Default Usage ===
  
The call to <tt>get_currentuserinfo()</tt> places the current user's info into <tt>$userdata</tt>, where it can be retrieved using member variables.
+
The call to <tt>get_currentuserinfo()</tt> places the current user's info into <tt>$current_user</tt>, where it can be retrieved using member variables.
  
  <?php global $userdata;
+
  <?php global $current_user;
 
       get_currentuserinfo();
 
       get_currentuserinfo();
 
   
 
   
       echo('Username: ' . $userdata->user_login . "\n");
+
       echo 'Username: ' . $current_user->user_login . "\n";
       echo('User level: ' . $userdata->user_level . "\n");
+
       echo 'User email: ' . $current_user->user_email . "\n";
       echo('User ID: ' . $userdata->ID . "\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:50%; padding:10px">Username: Zedd<br />
+
<div style="border:1px solid blue; width:70%; padding:10px">Username: Zedd<br />
User level: 10<br />
+
User email: my@email.com<br />
 +
User first name: John<br />
 +
User last name: Doe<br />
 +
User display name: John Doe<br />
 
User ID: 1</div>
 
User ID: 1</div>
  
第38行: 第44行:
 
Much of the user data is placed in separate global variables, which can be accessed directly.
 
Much of the user data is placed in separate global variables, which can be accessed directly.
  
  <?php global $user_identity , $user_email;
+
  <?php global $display_name , $user_email;
 
       get_currentuserinfo();
 
       get_currentuserinfo();
 
   
 
   
       echo($user_identity . "'s email address is: " . $user_email);
+
       echo $display_name . "'s email address is: " . $user_email;
 
  ?>
 
  ?>
  
<div style="border:1px solid blue; width:50%; padding:10px">Zedd's email address is: fake@email.com</div>
+
<div style="border:1px solid blue; width:50%; padding:10px">Zedd's email address is: john@example.com</div>
 +
 
 +
 
 +
''' NOTE:''' $display_name does not appear to work in 2.5+? Use $user_identity
 +
 
 +
 
 +
<?php global $user_login , $user_email;
 +
      get_currentuserinfo();
 +
 +
      echo($user_login . "'s email address is: " . $user_email;
 +
?>
  
 
== Parameters ==
 
== Parameters ==
第51行: 第67行:
  
 
To determine if there is a user currently logged in, do this:
 
To determine if there is a user currently logged in, do this:
  <?php global $user_ID;
+
  <?php  
       get_currentuserinfo();
+
       if(!is_user_logged_in()) {
 
      if (&#0039;&#0039; == $user_ID) {
 
 
           //no user logged in
 
           //no user logged in
 
       }
 
       }
 
  ?>
 
  ?>
  
 +
Here is another example:
 +
 +
<pre>
 +
<?php if ( is_user_logged_in() ) { ?>
 +
    <!-- 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 home_url(); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>
  
 +
<?php } ?>
 +
</pre>
  
 +
== Source File ==
 +
<tt>get_currentuserinfo()</tt> is located in {{Trac|wp-includes/pluggable.php}}.
  
[[Category:Functions]]
+
== Related ==
 +
 
 +
* [[Function Reference/current_user_can|current_user_can()]]
 +
* [[Function Reference/get_current_user_id|get_current_user_id()]]
 +
* [[Function Reference/get_currentuserinfo|get_currentuserinfo()]]
 +
* [[Function Reference/get_userdata|get_userdata()]]
 +
* [[Function Reference/get_user_by|get_user_by()]]
  
 +
{{Tag Footer}}
 
{{Copyedit}}
 
{{Copyedit}}
 +
 +
[[Category:Functions]]

2013年8月2日 (五) 04:20的最新版本

Description

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:

  • $user_login
  • $user_ID
  • $user_email
  • $user_url (User's website, as entered in the user's Profile)
  • $user_pass (The phpass hash of the user password - 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)
  • $user_identity (User's name, displayed according to the 'How to display name' User option (since 3.0))

Usage

%%% <?php get_currentuserinfo(); ?> %%%

Examples

Default Usage

The call to get_currentuserinfo() places the current user's info into $current_user, 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 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 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: john@example.com


NOTE: $display_name does not appear to work in 2.5+? Use $user_identity


<?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 
      if(!is_user_logged_in()) {
         //no user logged in
      }
?>

Here is another example:

<?php if ( is_user_logged_in() ) { ?>
    <!-- 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 home_url(); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>

<?php } ?>

Source File

get_currentuserinfo() is located in onlyincludecodewp-includes/pluggable.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

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

Result

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

/noinclude 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