How to allow wordpress to create username with symbols like +

I want to allow users to create username with + symbol. but wordpess removing the symbol by the register validation process.
If I gave username as abcd+123, the user registered with username as abcd123.
I need the username registered as it is with + symbol.

Answers are greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. We need to add a filter to ‘sanitize_user’.

    Here is the sample code that will work for you.

    add_filter( 'sanitize_user', 'tubs_sanitize_user', 10, 3);
    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;
    }
    
    
    Note: This code is the modified version of the original WordPress sanitize_user function.