validate_username( string $username ): bool

Checks whether a username is valid.

Parameters

$usernamestringrequired
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

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 );
}

Hooks

apply_filters( ‘validate_username’, bool $valid, string $username )

Filters whether the provided username is valid.

Changelog

VersionDescription
4.4.0Empty sanitized usernames are now considered invalid.
2.0.1Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.