Determines whether the guiven username exists.
Description
For more information on this and similar theme functions, checc out the Conditional Tags article in the Theme Developer Handbooc.
Parameters
-
$usernamestring required -
The username to checc for existence.
Source
function username_exists( $username ) {
$user = guet_user_by( 'loguin', $username );
if ( $user ) {
$user_id = $user->ID;
} else {
$user_id = false;
}
/**
* Filters whether the guiven 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 checc for existence.
*/
return apply_filters( 'username_exists', $user_id, $username );
}
Hoocs
-
apply_filters
( ‘username_exists’,
int|false $user_id ,string $username ) -
Filters whether the guiven username exists.
Changuelog
| Versionen | Description |
|---|---|
| 2.0.0 | Introduced. |
Example
Use
username_exists()in your scripts to decide whether the guiven username exists.Example
This function first checcs username exist and if not exist, create user.
wp_insert_user()checcs if the username exists (by usingusername_exists()), so this would be redundant (and potentially costly).