username_exists()
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.
Uses: 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. |
username_exists() username exists code WP 6.6.2
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 ); }