检索当前登陆用户的相关信息,并将信息置入$userdata全局变量。函数属性直接映射到数据库(参见 Database Description)中的wp_usrs表格。php
一样也将个体属性放置到如下独立全局变量中:数据库
<?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 } ?>