I'm working on a plugin needing to return avatars a specific way. If the user exists in the wp_users table the code is run else a different set of code is run if the user is not in the wp_users table. I cannot use the username_exist and am tinkering now with the idea of using the user_status value of 0.

From the Codex, code is possible to return information based on the user_status

$blogusers = get_users('user_status=0');
foreach ($blogusers as $user) {
    echo '<li>' . $user->user_email . '</li>';

This returns the email address for the users in the wp_users table.

But I want to use a conditional...

The following works on a local development server in which $id is 1. But not everyone using the plugin will have just this one user id ...

if ( $id == '1' ) {
    // echo 'User ID is ' . $id;
    // Do something here
} else {
    // the user is not in WordPress do something cool   

How can I use the user_status in a conditional to check if the user_status is 0 then do something else run the code the user not being in the WP users table.

In other words, how can I write a conditional to check user_status = 0?

2 Answers 2

$blogusers = get_users();
foreach ($blogusers as $user) {
        //do something
        // do something else
  • Thank you for the suggestion but that doesn't seem to work. The issue is the $user->user_status == '0' isn't allowing the else to run for non WP users.
    – LPH
    Mar 11, 2014 at 5:53
  • @LPH where do you store non wp users? what do you mean by non wp users? Mar 11, 2014 at 6:02
  • OK. Turns out I was setting the user_status to 0 in another function. Removed it. Now that code works with simply the if ($user->user_status == '0') Thank you for the help. Update -- Nope -- Typed too soon. This still doesn't work properly.
    – LPH
    Mar 11, 2014 at 6:03
  • @LPH you can accept the answer if use full. Mar 11, 2014 at 6:03
  • @RajeevVyas: Please don't pose a code block without any explanation. Edit your answer and say how the code can benefit. Mar 11, 2014 at 7:47

Did you try username_exists

       $username = $_POST['username'];

       // Check if username exist in wp_users table.
       if ( username_exists( $username ) ) {
           echo "Username In Use!";
       } else {
           echo "Username Not In Use!";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.