How To Have WordPress Check if a User Exists

When it comes to building web applications, I’m a fan of letting a user use their email as their primary login – in fact, I’d go as far as to say that I wish our emailĀ was our primary identity: They are unique, it’s a single thing to remember, and we all must have them in order to get online these days.

To that end, whenever I’m working on an application that requires a username and password, I always default to using the email address for the user’s identification. Everything else can be managed in a profile setting, right?

If you’re building an application in WordPress where you’re building custom registration mechanisms through your own views, validation, and so on, you may find yourself needing to check to see if a user already exists.

And if you – like me – often use email as the user name, there’s a really easy way to perform this check.

Have WordPress Check if a User Exists

Assuming that the email address is valid and is contained in the $_POST collection, you can do this:

// First, make sure the email address is set
if ( isset( $_POST['email-address'] ) && ! empty( $_POST['email-address'] ) ) {

  // Next, sanitize the data
  $email_addr = trim( strip_tags( stripslashes( $_POST['email-address'] ) ) );

  // Now use PHP to check if it's a valid email format
  if ( filter_var( $email_address, FILTER_VALIDATE_EMAIL ) ) {

    if( false == get_user_by( 'email', $email_addr ) ) {
      // The user doesn't exist
    } else {
      // The user exists
    } // end if/else

  } else {

    // An invalid email format has been entered

  } // end if/else

} // end if

Now, to be clear, this is simply demonstration code. I believe that there are cleaner ways to go about writing this type of functionality through the use of helper functions, fewer conditionals, and so on.

But this post isn’t really about the best way to architect this solution. Instead, it’s simply demonstrating a way that you can check to see if a WordPress user already exists by a specified email address and, in this case, that method is get_user_by.

What About Other Credentials?

get_user_by in The WordPress Codex

get_user_by in The WordPress Codex

Honestly, this function is flexible and is well documented in the WordPress Codex.

You can actually use a person’s email, the user ID, the slug, or their login name to check for their existence in the system.

Next time you’ve got to determine if a user already exists and you have at least one piece of information about the account that you’re trying to create, check out this function.

7 Comments

If username (used in login/password) conflicts after email is valid for registration, what to do ?
My code gets username from Facebook and registers user on wordpress blog. It is third party FB connect code so beyond me to change how they create users. I wish to write my own code to create user after authenticated from facebook

    If you’re pulling the information from Facebook’s API, I would guess that you could create a username based on some of that information.

    For example, perhaps the users email address or the user’s Facebook profile ID number or something like that can serve as a username. You can also just programmatically generate the password and then insert the user.

    When the user revisits the site and they’re logged into Facebook, you can authenticate by that information on the server side.

Do you know if this can be used to check and create a wordpress account if needed when someone is logging into phpBB?

    If you’re looking to create a WordPress account when someone has logged into phpBB, you’d need to setup some type of script or program that would take the credentials from phpBB and then shoot it over to WordPress.

    It’s possible to do, but would require a little bit of work in you creating the “middle-man,” so to speak that has the two systems talking to one another.

      Hi Tom,

      I managed to get it going, someone on wp-hackers given me something to start from


      include '/../../../wp-load.php';
      if(!username_exists($username))
      {
      $args = array(
      'user_pass'=>$password,
      'user_login'=>$username,
      'user_nicename'=>$username,
      'user_email'=>$user->data['user_email'],
      'display_name'=>$username,
      'role'=>"subscriber"
      );
      @define('WP_IMPORTING', true);
      $importwp = wp_insert_user($args);
      update_user_meta($importwp, 'phpbb_userid', $user->data['user_id']);
      }

      There one small bug but I’m sure the guys at phpbb.com will help me out with it.

      Thank-you Tom :)

Forgot to select the email option

Leave a Reply

Name and email address are required. Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>