通常做法
WordPress WordPress 5.7.1以后的版本想要添加中文用户名,请配合使用插件:WordPress Special Characters in Usernames
在网上浏览了很多篇文章,发现都是千篇一律的转载,然后又去谷歌搜索了下英文答案,发现大家的普遍做法是安装支持非拉丁文的插件:WordPress Special Characters in Usernames,唯独遗憾的就是不支持中文,但可以接触非拉丁文输入限制,再配合国内的代码片段,可以做到支持中文用户名输入,安装好上面插件后,将以下php代码复制到当前主题目录下的functions.php中,即可让WordPress支持使用中文用户名注册和登录:
代码片段
function ludou_sanitize_user ($username, $raw_username, $strict) {
$username = wp_strip_all_tags( $raw_username );
$username = remove_accents( $username );
// Kill octets
$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
$username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
// 网上很多教程都是直接将$strict赋值false,
// 这样会绕过字符串检查,留下隐患
if ($strict) {
$username = preg_replace ('|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username);
}
$username = trim( $username );
// Consolidate contiguous whitespace
$username = preg_replace( '|\s+|', ' ', $username );
return $username;
}
add_filter ('sanitize_user', 'ludou_sanitize_user', 10, 3);
代码复制到当前主题最下面位置:

然后重启服务器。即可实现添加

好了,到这教程全部结束,如有疑问,请加微信:laolangcy 咨询!