• Resolved thomei

    (@thomei)


    Hi
    often we see user registrations with their e-mail-address as username. We would like to force new users to user a username different form their e-mail-address.

    How to filter (reject) usernames with “@” (at) in it, while registration?

    • This topic was modified 4 years, 8 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Thread Starter thomei

    (@thomei)

    Thank you very much for the replay! But this plugin is an overkill.

    I found a more efficient solution.
    (We are running BuddyPress.)
    I added to functions.php:

    <?php
    
    add_action( 'bp_loaded','bpdev_remove_bp_pre_user_login_action') ;
    function bpdev_remove_bp_pre_user_login_action(){
       
      remove_action( 'pre_user_login', 'bp_core_strip_username_ats' );
    }
    
    add_filter( 'validate_username','bpdev_restrict_at_in_username',10,2) ;
    function bpdev_restrict_at_in_username( $valid,$user_name ){
    
      if ( preg_match('/@/',$user_name ) )
      return false;
      return $valid;
    }

    It’s working. But one issue is left:

    But I’m still looking for a solution to change the error message to the user, because it says “@” is allowed.

    • This reply was modified 4 years, 8 months ago by thomei.
    • This reply was modified 4 years, 8 months ago by thomei.
    • This reply was modified 4 years, 8 months ago by thomei.
    Thread Starter thomei

    (@thomei)

    SOLVED!

    The following code in functions.php does it:

    
    function validate_username_noat (){
    
        global $bp;
    
        $username = $bp->signup->username;
    
        if ($username){
    
            $tld_index = strrpos($username,'.');
            $tld = substr($username,$tld_index);
    
            if ($tld != '.edu'){
                $bp->signup->errors['signup_username'] = 'Benutzernamen k?nnen nur Buchstaben, Zahlen, "." und "-" enthalten.';
            }
    
        }
    
    }
    
    add_action( 'bp_loaded','bpdev_remove_bp_pre_user_login_action') ;
    function bpdev_remove_bp_pre_user_login_action(){
       
      remove_action( 'pre_user_login', 'bp_core_strip_username_ats' );
    }
    
    add_filter( 'validate_username','bpdev_restrict_at_in_username',10,2) ;
    function bpdev_restrict_at_in_username( $valid,$user_name ){
    
      if ( preg_match('/@/',$user_name ) )
      return false;
      return $valid;
    }
    
    
    Thread Starter thomei

    (@thomei)

    Or even a better solution:

    
    function username_noat_validation() {
            global $bp;
    
            if ( !empty( $_POST['signup_username'] ) )
            if ( !valid_noat_username( $_POST['signup_username'] ) ){
                $bp->signup->errors['signup_username'] = __( 'The @-symbol is not possible in the user name.', 'buddypress' );
            }
    }
    add_action( 'bp_signup_validate', 'username_noat_validation');
    
    function valid_noat_username($ucandidate) {
            $r1='/@/';  //At
            if(preg_match_all($r1,$ucandidate, $o)<1) return TRUE;
            
            return FALSE;
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to filter usernames with “@” in it?’ is closed to new replies.