get_user_by()通过字段信息获取用户

目录

描述

Get user data by field and data. The possible fields are shown below with the corresponding columns in the wp_users database table.

  • 'id' - ID
  • 'slug' - user_nicename
  • 'email' - user_email
  • 'login' - user_login

get_user_by() 描述

用法

<?php $user get_user_by$field$value ); ?>

get_user_by() 用法

参数

$field

(string) (必填) 'id', 'slug', 'email', or 'login'

默认值: None

$value

(string|integer) (必填) Search for this field value

默认值: None

get_user_by() 参数

返回值

(mixed) 

WP_User object or false if no user is found. Will also return false if $field does not exist.

get_user_by() 返回值

示例

Default usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$user = get_user_by( 'email', 'user@example.com' );
echo 'User is ' . $user->first_name . ' ' . $user->last_name;

Sample response of $author_obj = get_user_by('id', 1);

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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
}

get_user_by() 示例

注意

历史

get_user_by() 历史

源文件

get_user_by() 函数的代码位于 wp-includes/pluggable.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve user info by a given field
 *
 * @since 2.8.0
 *
 * @param string     $field The field to retrieve the user with. id | slug | email | login
 * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
 * @return WP_User|false WP_User object on success, false on failure.
 */
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;
}
endif;

if ( !function_exists('cache_users') ) :

get_user_by() 源文件

相关