Software, Development, and WordPress

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_addr, FILTER_VALIDATE_EMAIL ) ) {

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

  } else {

    // An invalid email format has been entered



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.


  1. heena

    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

    • Tom McFarlin

      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.

  2. Danielx64

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

    • Tom McFarlin

      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.

      • Danielx64

        Hi Tom,

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

        include '/../../../wp-load.php';
        $args = array(
        @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 will help me out with it.

        Thank-you Tom :)

  3. Danielx64

    Forgot to select the email option

  4. Lindsay

    Thanks for this! It was just what I needed. Note — I think there’s a typo in the code above. In line 8, “$email_address” should be “$email_addr” to match the name you used for that variable in line 5.

    • Tom

      Gotcha! Thanks for the tip – I’ll be sure to fix the code :).

Leave a Reply

© 2020 Tom McFarlin

Theme by Anders NorenUp ↑