That doesn’t help me at all, it only forces specific characters inside the username it doesn’t actually allow the option of them.
Try with this:
function tubs_sanitize_user($username, $raw_username, $strict) {
$new_username = strip_tags($raw_username);
// Kill octets
$new_username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $new_username);
$new_username = preg_replace('/&.?;/', '', $new_username); // Kill entities
// If strict, reduce to ASCII for max portability.
if ( $strict )
$new_username = preg_replace('|[^a-z0-9 _.\-@+]|i', '', $new_username);
return $new_username;
}
add_filter( 'sanitize_user', 'tubs_sanitize_user', 10, 3);
Source
Or try with this:
// Force Strong Username
function strong_username() {
global $bp;
if ( !empty( $_POST['signup_username'] ) )
if ( !valid_username( $_POST['signup_username'] ) ){
$bp->signup->errors['signup_username'] = __( 'Your username is not strong enough. Use uppercase, lowercase, numbers and special chars like - _', 'bp-strong-username-password', 'buddypress' );
}
}
add_action( 'bp_signup_validate', 'strong_username');
function valid_username($candidate) {
$r1='/[A-Z]/'; //Uppercase
$r2='/[a-z]/'; //lowercase
$r3='/[0-9]/'; //numbers
$r4='/[-_]/'; //Special chars, underscore...
if(preg_match_all($r1,$candidate, $o)<1) return FALSE;
if(preg_match_all($r2,$candidate, $o)<1) return FALSE;
if(preg_match_all($r3,$candidate, $o)<1) return FALSE;
if(preg_match_all($r4,$candidate, $o)<1) return FALSE;
if(strlen($candidate)<6) return FALSE;
return TRUE;
}
Here’s my solution, bit easier:
function blockode_custom_username_validate( $result ) {
$error_name = $result[ 'errors' ]->get_error_message( 'user_name' );
if ( ! empty ( $error_name )
&& !preg_match( '/[^_a-zA-Z0-9]/', $result['user_name'] )
) {
unset ( $result[ 'errors' ]->errors[ 'user_name' ] );
return $result;
}
else {
return $result;
}
}
add_filter( 'wpmu_validate_user_signup', 'blockode_custom_username_validate' );
Here’s my solution:
add_filter( 'wpmu_validate_user_signup', 'my_wpmu_validate_user_signup' );
function my_wpmu_validate_user_signup( $result ) {
$underscores_error_message = __( 'Sorry, usernames may not contain the character "_"!', 'buddypress' );
$error_messages_array = $result[ 'errors' ]->get_error_messages('user_name');
if ( count($error_messages_array) == 1 and $error_messages_array[0] == $underscores_error_message ) {
$result[ 'errors' ]->remove('user_name');
}
return $result;
}