Created
September 28, 2023 08:35
-
-
Save sbrajesh/566b048a8fb8adcef740f5bebe9f935b to your computer and use it in GitHub Desktop.
Disallows special characters in BuddyPress username
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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