How to check user exists in WordPress site ?


You can use wordpress’s inbuilt function ‘get_userdata’ to detect a user is exists or not. This function return WP_User object if a user found in WordPress otherwise return false value.
Following code shows how to detect a user in wordpress using id.

<?php
$userid = 1; //for example '1' is used here
$userdata = get_userdata( $userid );

if($userdata){
echo '<pre>';
print_r($userdata->data);
echo '</pre>';
} else {
echo 'user not found';
}
?>

Output
=======

stdClass Object
(
[ID] => 1
[user_login] => admin
[user_pass] => $A$BWiIHwSw1/Stxn2tTEaNUlGmDK670b9
[user_nicename] => admin
[user_email] => admin@gmail.com
[user_url] =>
[user_registered] => 2013-05-10 17:32:42
[user_activation_key] =>
[user_status] => 0
[display_name] => admin
)

You can also validate user using email address. get_user_by function is useful to validate user by id,slug,email and login.

$email = 'testemail@test.com';
$user_data = get_user_by( 'email', $email ); // return WP_User object otherwise return false if not found 
if ( empty( $user_data ) ) {	
	echo 'User not exits with this email address';
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s