WordPress开发中经常要用到用户信息的函数wp_get_current_user(),那么wp_get_current_user()能获取到些什么返回结果呢?

阿柳云推荐

【爆款推荐】高性能服务器,就选阿里云

全国销量领先 高性价比,多并发,好网站都用它


在页面中查看,可以看到以下参数:

object(WP_User)#3821 (8) {
  ["data"]=>
  object(stdClass)#3818 (10) {
    ["ID"]=>
    string(1) "1"
    ["user_login"]=>
    string(9) "zhuxijing"
    ["user_pass"]=>
    string(34) "$88888888888888QB0"
    ["user_nicename"]=>
    string(9) "zhuxijing"
    ["user_email"]=>
    string(16) "admin@aliuyun.cn"
    ["user_url"]=>
    string(0) ""
    ["user_registered"]=>
    string(19) "2019-06-10 14:31:59"
    ["user_activation_key"]=>
    string(0) ""
    ["user_status"]=>
    string(1) "0"
    ["display_name"]=>
    string(9) "zhuxijing"
  }
  ["ID"]=>
  int(1)
  ["caps"]=>
  array(1) {
    ["administrator"]=>
    bool(true)
  }
  ["cap_key"]=>
  string(15) "wp_capabilities"
  ["roles"]=>
  array(1) {
    [0]=>
    string(13) "administrator"
  }
  ["allcaps"]=>
  array(62) {
    ["switch_themes"]=>
    bool(true)
    ["edit_themes"]=>
    bool(true)
    ["activate_plugins"]=>
    bool(true)
    ["edit_plugins"]=>
    bool(true)
    ["edit_users"]=>
    bool(true)
    ["edit_files"]=>
    bool(true)
    ["manage_options"]=>
    bool(true)
    ["moderate_comments"]=>
    bool(true)
    ["manage_categories"]=>
    bool(true)
    ["manage_links"]=>
    bool(true)
    ["upload_files"]=>
    bool(true)
    ["import"]=>
    bool(true)
    ["unfiltered_html"]=>
    bool(true)
    ["edit_posts"]=>
    bool(true)
    ["edit_others_posts"]=>
    bool(true)
    ["edit_published_posts"]=>
    bool(true)
    ["publish_posts"]=>
    bool(true)
    ["edit_pages"]=>
    bool(true)
    ["read"]=>
    bool(true)
    ["level_10"]=>
    bool(true)
    ["level_9"]=>
    bool(true)
    ["level_8"]=>
    bool(true)
    ["level_7"]=>
    bool(true)
    ["level_6"]=>
    bool(true)
    ["level_5"]=>
    bool(true)
    ["level_4"]=>
    bool(true)
    ["level_3"]=>
    bool(true)
    ["level_2"]=>
    bool(true)
    ["level_1"]=>
    bool(true)
    ["level_0"]=>
    bool(true)
    ["edit_others_pages"]=>
    bool(true)
    ["edit_published_pages"]=>
    bool(true)
    ["publish_pages"]=>
    bool(true)
    ["delete_pages"]=>
    bool(true)
    ["delete_others_pages"]=>
    bool(true)
    ["delete_published_pages"]=>
    bool(true)
    ["delete_posts"]=>
    bool(true)
    ["delete_others_posts"]=>
    bool(true)
    ["delete_published_posts"]=>
    bool(true)
    ["delete_private_posts"]=>
    bool(true)
    ["edit_private_posts"]=>
    bool(true)
    ["read_private_posts"]=>
    bool(true)
    ["delete_private_pages"]=>
    bool(true)
    ["edit_private_pages"]=>
    bool(true)
    ["read_private_pages"]=>
    bool(true)
    ["delete_users"]=>
    bool(true)
    ["create_users"]=>
    bool(true)
    ["unfiltered_upload"]=>
    bool(true)
    ["edit_dashboard"]=>
    bool(true)
    ["update_plugins"]=>
    bool(true)
    ["delete_plugins"]=>
    bool(true)
    ["install_plugins"]=>
    bool(true)
    ["update_themes"]=>
    bool(true)
    ["install_themes"]=>
    bool(true)
    ["update_core"]=>
    bool(true)
    ["list_users"]=>
    bool(true)
    ["remove_users"]=>
    bool(true)
    ["promote_users"]=>
    bool(true)
    ["edit_theme_options"]=>
    bool(true)
    ["delete_themes"]=>
    bool(true)
    ["export"]=>
    bool(true)
    ["administrator"]=>
    bool(true)
  }
  ["filter"]=>
  NULL
  ["site_id":"WP_User":private]=>
  int(1)
}

这些信息是不是足够你使用了呢?

那么得到$user,如何获取用户名?

display_name; ?>

是不是很简单。

参与评论