BuddyDev

Search

[Resolved] Username changer allows spaces in username – how can this be blocked

  • Participant
    Level: Initiated
    Posts: 1
    Gordon White on #34365

    Hi,

    The BuddyPress Username Change plug in allows a user to change their user name to to include a space – e.g. “User Name”

    This is causing issues in other areas of the site… is there a way to prevent usernames having a space in them? (and possibly also being able to exlude other characters which could cause invalid results e.g /\*%@ could cause other issues

    Thanks!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2908
    Ravi on #34369

    Hello Gordon,

    Thank you for posting. Please try the following code. It will allow space in username. You can find more regular expressions to avoid special characters.

    
    
    /**
     * Validate user name
     *
     * @param WP_Error $error Error object.
     * @param string   $new_user_name New user name.
     *
     * @return string
     */
    function buddydev_validate_user_name( $error, $new_user_name ) {
    
    	if ( $error->has_errors() ) {
    		return $error;
    	}
    
    	if ( preg_match('/\s/',$new_user_name ) ) {
    		$error->add( 'contains_spaces', __( 'User name can not have space in it' ) );
    	}
    
    	return $error;
    }
    add_filter( 'bp_username_changer_validation_errors', 'buddydev_validate_user_name', 10, 2 );
    
    

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 1
    Gordon White on #34564

    Thanks! That worked.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2908
    Ravi on #34565

    Hello Gordon,

    Thank you for the acknowledgment.

    Regards
    Ravi

The topic ‘ [Resolved] Username changer allows spaces in username – how can this be blocked’ is closed to new replies.

This topic is: resolved