validate_username() WordPress Function
The validate_username() function is used to check whether a username is valid. It is used when a new user is registering on a WordPress site. This function checks the length of the username and whether it contains only alphanumeric characters. If the username is valid, it returns true. Otherwise, it returns false.
validate_username( string $username ) #
Checks whether a username is valid.
Parameters
- $username
(string)(Required)Username.
Return
(bool) Whether username given is valid.
More Information
This function attempts to sanitize the username, and if it “passes”, the name is considered valid. For additional logic, you can use the ‘validate_username
‘ hook.
Source
File: wp-includes/user.php
function validate_username( $username ) { $sanitized = sanitize_user( $username, true ); $valid = ( $sanitized == $username && ! empty( $sanitized ) ); /** * Filters whether the provided username is valid. * * @since 2.0.1 * * @param bool $valid Whether given username is valid. * @param string $username Username to check. */ return apply_filters( 'validate_username', $valid, $username ); }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.4.0 | Empty sanitized usernames are now considered invalid. |
2.0.1 | Introduced. |