wordpress經常使用函數-get_currentuserinfo()

說明

檢索當前登陸用戶的相關信息,並將信息置入$userdata全局變量。函數屬性直接映射到數據庫(參見 Database Description)中的wp_usrs表格。php

一樣也將個體屬性放置到如下獨立全局變量中:數據庫

  • $user_login
  • $user_level
  • $user_ID
  • $user_email
  • $user_url(用戶在用戶資料中輸入的網址)
  • $user_pass_md5 (用戶密碼的md5 hash——一種幾乎沒法解密(不是徹底沒法解密)的編碼,可比較輸入提示口令與實際用戶密碼的不一樣)
  • $display_name(用戶名,根據'How to display name'用戶選項結果進行顯示)

用法

<?php get_currentuserinfo(); ?>wordpress

示例

缺省用法

調用get_currentuserinfo()將當前用戶信息放入$userdata,可用成員變量在$userdata中檢索用戶信息。函數

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

User ID: 1this

使用獨立全局變量

用戶資料大多存放在單個全局變量中,可直接訪問。編碼

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

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

Zedd's email address is: fake@email.comurl

注意:$display_name彷佛沒法在2.5以上版本中運行。$user_login運行良好。插件

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

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

參數

該函數不接受任何參數。code

檢查當前是否有已登陸用戶,執行如下代碼:

<?php global $user_ID;
      get_currentuserinfo();          

      if ('' == $user_ID) {
         //no user logged in
      }
?>

下面仍然是一個IF STATEMENT示例,用在側邊條中,參照ttp://www.kriesi.at/archives/wordpress-plugin-my-favorite-posts 中的"My Fav"插件。

<?php if ( $user_ID ) { ?>
<!-- enter info that logged in users will see -->
<!-- in this case im running a bit of php to a plugin -->    

<?php mfp_display(); ?>    

<?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 } ?>
相關文章
相關標籤/搜索