Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrajesh/566b048a8fb8adcef740f5bebe9f935b to your computer and use it in GitHub Desktop.
Save sbrajesh/566b048a8fb8adcef740f5bebe9f935b to your computer and use it in GitHub Desktop.
Disallows special characters in BuddyPress username
/**
* Disallow special characters in BuddyPres username.
*/
add_filter( 'bp_core_validate_user_signup', function ( $results ) {
if ( empty( $results ) || empty( $results['errors'] ) ) {
return $results;
}
$username = $results['user_name'];
$invalid_characters = array(
'@' => '',
'.' => '',
'-' => '',
);
$sanitized = str_replace( array_keys( $invalid_characters ), array_values( $invalid_characters ), $username );
if ( $sanitized !== $username ) {
$results['errors']->add( 'user_name', sprintf( 'Special characters %s are not allowed!', join( ',', array_keys( $invalid_characters ) ) ) );
}
return $results;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment