返回介绍

get_user_by()

发布于 2017-09-11 00:43:52 字数 9136 浏览 1367 评论 0 收藏 0

get_user_by( string $field,  int|string $value )

Retrieve user info by a given field


description


参数

$field

(string) (Required) The field to retrieve the user with. id | ID | slug | email | login.

$value

(int|string) (Required) A value for $field. A user ID, slug, email address, or login name.


返回值

(WP_User|false) WP_User object on success, false on failure.


源代码

File: wp-includes/pluggable.php

function get_user_by( $field, $value ) {
	$userdata = WP_User::get_data_by( $field, $value );

	if ( !$userdata )
		return false;

	$user = new WP_User;
	$user->init( $userdata );

	return $user;
}

更新日志

Versiondescription
4.4.0Added 'ID' as an alias of 'id' for the $field parameter.
2.8.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-user.php: WP_User::get_data_by()
  • wp-includes/class-wp-user.php: WP_User::__construct()

Used By

  • wp-includes/l10n.php: get_user_locale()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::update_item()
  • wp-includes/user.php: wp_authenticate_email_password()
  • wp-includes/link-template.php: get_avatar_data()
  • wp-login.php: retrieve_password()
  • wp-admin/includes/schema.php: populate_network()
  • wp-includes/capabilities.php: map_meta_cap()
  • wp-includes/pluggable.php: wp_validate_auth_cookie()
  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/deprecated.php: get_profile()
  • wp-includes/class-wp-query.php: WP_Query::get_posts()
  • wp-includes/user.php: username_exists()
  • wp-includes/user.php: email_exists()
  • wp-includes/user.php: wp_authenticate_username_password()
  • wp-includes/pluggable-deprecated.php: get_userdatabylogin()
  • wp-includes/pluggable-deprecated.php: get_user_by_email()
  • wp-includes/pluggable-deprecated.php: wp_setcookie()
  • wp-includes/post.php: get_pages()
  • wp-includes/ms-functions.php: is_user_spammy()
  • wp-includes/ms-functions.php: wpmu_signup_blog_notification()
  • wp-includes/ms-functions.php: wpmu_signup_user_notification()
  • wp-includes/ms-deprecated.php: is_site_admin()
  • wp-includes/ms-deprecated.php: get_user_details()
  • wp-includes/ms-deprecated.php: get_user_id_from_string()
  • wp-includes/comment.php: check_comment()
  • Show 21 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 10You must log in to vote on the helpfulness of this note Contributed by Codex

    Default usage

    
    $user = get_user_by( 'email', 'user@example.com' );
    echo 'User is ' . $user->first_name . ' ' . $user->last_name;
    
    
  2. I always prefer to check if the variable ($user) is empty or not before echoing any properties of the $user object:

    $user = get_user_by( ’email’, ‘user@example.com’ );

    if ( ! empty( $user ) ) {
    echo ‘User is ‘ . $user->first_name . ‘ ‘ . $user->last_name;
    }

    This is to keep things clean and prevent error messages and warnings.

    Sample response of

    
    $author_obj = get_user_by('id', 1);
    


    object(WP_User)#96 (7) {
    ["data"]=>
    object(stdClass)#95 (10) {
    ["ID"]=>
    string(1) "1"
    ["user_login"]=>
    string(10) "superadmin"
    ["user_pass"]=>
    string(34) "$P$Bx0Jgq6/Qw/o3A5fXcVKg4jxQr2PAB1"
    ["user_nicename"]=>
    string(10) "superadmin"
    ["user_email"]=>
    string(16) "user@example.com"
    ["user_url"]=>
    string(0) ""
    ["user_registered"]=>
    string(19) "2014-02-21 21:31:40"
    ["user_activation_key"]=>
    string(0) ""
    ["user_status"]=>
    string(1) "0"
    ["display_name"]=>
    string(10) "superadmin"
    }
    ["ID"]=>
    int(1)
    ["caps"]=>
    array(1) {
    ["administrator"]=>
    bool(true)
    }
    ["cap_key"]=>
    string(25) "wp_capabilities"
    ["roles"]=>
    array(1) {
    [0]=>
    string(13) "administrator"
    }
    ["allcaps"]=>
    array(63) {
    ["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)
    ["add_users"]=>
    bool(true)
    ["promote_users"]=>
    bool(true)
    ["edit_theme_options"]=>
    bool(true)
    ["delete_themes"]=>
    bool(true)
    ["export"]=>
    bool(true)
    ["administrator"]=>
    bool(true)
    }
    ["filter"]=>
    NULL
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文