When we are working in wordpress, sometimes we need to check if a username exists in our database or not. In this case we have a username and we want to check whether it is in our database or associated with a email id or not.

We have a default wordpress function for it :


<?php 
username_exists( $user_name ); //replace $user_name with our username 

//it is required to put $user_name value 
?>

We can use it in with if else statements in below mentioned way :

<?php
 $user_name = 'siteuser';
       if ( username_exists( $user_name ) ){

           echo "We have this username";
       
      }else {

           echo "This is not exists";

?>

Basically this functions return user ID if it exists only, it returns NULL when a given username not exists. Similarly we can check for the email id like this : Check a email exists in wordpress and to check if a email is valid or not like : Check email is valid or not in wordpress

Enjoy coding in wordpress 🙂