username_exists() WP 2.0.0
Checks whether the given username (login) exists.
Checks the presence of the specified username (login) in the user's database. The search passes through the user_login field.
The function requires file:
require_once ABSPATH . WPINC . '/user.php';
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Works based on: get_user_by()
Hooks from the function
Return
Int/false. The user ID on success, false on failure.
Usage
username_exists( $username );
- $username(string) (required)
- The username to check for existence.
Examples
#1 Example of username verification when registering a new user:
$username = $_POST['username']; if ( username_exists( $username ) ) echo 'Username is already in use!'; else echo 'You can use this username!';
Changelog
Since 2.0.0 | Introduced. |
Code of username_exists() username exists WP 5.6
function username_exists( $username ) {
$user = get_user_by( 'login', $username );
if ( $user ) {
$user_id = $user->ID;
} else {
$user_id = false;
}
/**
* Filters whether the given username exists.
*
* @since 4.9.0
*
* @param int|false $user_id The user ID associated with the username,
* or false if the username does not exist.
* @param string $username The username to check for existence.
*/
return apply_filters( 'username_exists', $user_id, $username );
}Related Functions
From category: Other
- auth_redirect()
- count_many_users_posts()
- count_user_posts()
- count_users()
- email_exists()
- get_author_posts_url()
- get_current_user_id()
- get_editable_roles()
- get_the_author()