add_filter( 'validate_username', 'my_no_space_username', 10, 2 );
/**
* Checks to see if a username contains whitespace.
* @see: https://developer.wordpress.org/reference/functions/validate_username/
*
* @param boolean $valid True if the username is valid, otherwise false
* @param string $username The username to check
*/
function my_no_space_username( $valid, $username ) {
if ( preg_match( '/\s/', $username ) ) {
// contains a space
return false;
}
return $valid;
}
Disallow spaces in WordPress usernames
This post brought to you by RocketGeek, ButlerBlog, and the following: