这是Gonzalo过滤器的一个更安全的版本,它只会删除“用户名只能包含小写字母(a-z)和数字”的验证错误。相反,它使用类似的验证,但允许在sanitize\\u email()中看到相同的字符。
唯一需要注意的是,这将检查错误消息名称。如果WordPress将来更改该错误消息,则将停止工作。但这是针对特定验证消息的唯一方法。
/**
* Allow users to sign up using an email address as their username.
* Removes the default restriction of [a-z0-9] and replaces it with [a-z0-9+_.@-].
*
* @param $result
*
* @return array $result
*/
function wpse_295037_disable_username_character_type_restriction( $result ) {
$errors = $result[\'errors\'];
$user_name = $result[\'user_name\'];
// The error message to look for. This should exactly match the error message from ms-functions.php -> wpmu_validate_user_signup().
$error_message = __( \'Usernames can only contain lowercase letters (a-z) and numbers.\' );
// Look through the errors for the above message.
if ( !empty($errors->errors[\'user_name\']) ) foreach( $errors->errors[\'user_name\'] as $i => $message ) {
// Check if it\'s the right error message.
if ( $message === $error_message ) {
// Remove the error message.
unset($errors->errors[\'user_name\'][$i]);
// Validate using different allowed characters based on sanitize_email().
$pattern = "/[^a-z0-9+_.@-]/i";
if ( preg_match( $pattern, $user_name ) ) {
$errors->add( \'user_name\', __( \'Username is invalid. Usernames can only contain: lowercase letters, numbers, and these symbols: <code>+ _ . @ -</code>.\' ) );
}
// If there are no errors remaining, remove the error code
if ( empty($errors->errors[\'user_name\']) ) {
unset($errors->errors[\'user_name\']);
}
}
}
return $result;
}
add_filter( \'wpmu_validate_user_signup\', \'wpse_295037_disable_username_character_type_restriction\', 20 );