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.

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

echo '<pre>';
echo '</pre>';
} else {
echo 'user not found';


stdClass Object
[ID] => 1
[user_login] => admin
[user_pass] => $A$BWiIHwSw1/Stxn2tTEaNUlGmDK670b9
[user_nicename] => admin
[user_email] =>
[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 = '';
$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';

Leave a Reply

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

You are commenting using your 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