This looks like it might be helpful: https://wordpress.org/plugins/restrict-usernames-emails-characters/
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.
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;
}
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;
}