get_user_by()WP 2.8.0

Gets the user by the specified field and the value of this field (by ID, login, mail).

Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.

Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.

Uses: WP_User()
1 time — 0.0016251 sec (very slow) | 50000 times — 1.63 sec (fast) | PHP 7.4.8, WP 5.6

No Hooks.

Return

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

Usage

get_user_by( $field, $value );
$field(string) (required)

The field to retrieve the user with. May be:

  • id or ID - by ID field.
  • slug - by user_nicename field.
  • email - by user_email field.
  • login - by user_login field.
$value(string/int) (required)
The value of specified string.

Examples

0

#1 Retrieve the user by email

Example, a native WP function that uses get_user_by(). The function gets the user by e-mail address, if the user exists, his ID will be returned.

function email_exists( $email ) {
	if ( $user = get_user_by('email', $email) )
		return $user->ID;

	return false;
}
0

#2 Retrieve the user by id

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

$user will contains:

WP_User Object
(
	[data] => stdClass Object
		(
			[ID] => 1
			[user_login] => kama
			[user_pass] => $P$Bu354452345AfDy0g3453245345345J/
			[user_nicename] => kama
			[user_email] => [email protected]
			[user_url] =>
			[user_registered] => 2010-03-26 09:27:40
			[user_activation_key] =>
			[user_status] =>
			[display_name] => Kama
		)

	[ID] => 1
	[caps] => Array
		(
			[administrator] => 1
		)

	[cap_key] => wp_capabilities
	[roles] => Array
		(
			[0] => administrator
		)

	[allcaps] => Array
		(
			[switch_themes] => 1
			[edit_themes] => 1
			[activate_plugins] => 1
			[edit_plugins] => 1
			[edit_users] => 1
			[edit_files] => 1
			[manage_options] => 1
			[moderate_comments] => 1
			[manage_categories] => 1
			[manage_links] => 1
			[upload_files] => 1
			[import] => 1
			[unfiltered_html] => 1
			[edit_posts] => 1
			[edit_others_posts] => 1
			[edit_published_posts] => 1
			[publish_posts] => 1
			[edit_pages] => 1
			[read] => 1
			[level_10] => 1
			[level_9] => 1
			[level_8] => 1
			[level_7] => 1
			[level_6] => 1
			[level_5] => 1
			[level_4] => 1
			[level_3] => 1
			[level_2] => 1
			[level_1] => 1
			[level_0] => 1
			[edit_others_pages] => 1
			[edit_published_pages] => 1
			[publish_pages] => 1
			[delete_pages] => 1
			[delete_others_pages] => 1
			[delete_published_pages] => 1
			[delete_posts] => 1
			[delete_others_posts] => 1
			[delete_published_posts] => 1
			[delete_private_posts] => 1
			[edit_private_posts] => 1
			[read_private_posts] => 1
			[delete_private_pages] => 1
			[edit_private_pages] => 1
			[read_private_pages] => 1
			[delete_users] => 1
			[create_users] => 1
			[unfiltered_upload] => 1
			[edit_dashboard] => 1
			[update_plugins] => 1
			[delete_plugins] => 1
			[install_plugins] => 1
			[update_themes] => 1
			[install_themes] => 1
			[manage_downloads] => 1
			[manage_database] => 1
			[democracy_admin] => 1
			[update_core] => 1
			[list_users] => 1
			[remove_users] => 1
			[add_users] => 1
			[promote_users] => 1
			[edit_theme_options] => 1
			[delete_themes] => 1
			[export] => 1
			[administrator] => 1
		)

	[filter] =>
)

Notes

  • Global. WP_User. $current_user The current user object which holds the user data.

Changelog

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

get_user_by() code WP 6.4.3

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;
}