Skip to:
Content
Pages
Categories
Search
Top
Bottom

Allow Underscores in Username


  • vecturn
    Participant

    @vecturn

    So ever since buddypress was installed users can only register accounts with lowercase usernames and just alphanumeric but I want to add the ability for users to sign up with capital letters in their usernames as well as an underscore if needed.

    I have already tried the “Network Username Overrides” plugin which doesn’t seem to change anything else I still get the error message no matter what settings I use for that plugin.

    Any ideas how to fix this preferably without modifying the core?

Viewing 5 replies - 1 through 5 (of 5 total)

  • livingflame
    Participant

    @livingflame


    vecturn
    Participant

    @vecturn

    That doesn’t help me at all, it only forces specific characters inside the username it doesn’t actually allow the option of them.


    livingflame
    Participant

    @livingflame

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

    vecturn
    Participant

    @vecturn

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

    Carlo Tafuro
    Participant

    @carlotafuro

    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;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar