一般而言,我们在网上注册用户名的时候肯定常用和默认使用英文或者数字作为注册用户名,同样在WordPress注册账户的时候也是这样默认的。我们一般的WordPress程序可能都没有开放用户注册,也有一些朋友是开放注册的。如果我们希望支持用户注册使用中文用户名,则需要在WordPress程序中设置才可以支持。
将以下php代码复制到当前主题目录下的functions.php中,即可让WordPress支持使用中文用户名注册和登录:
function wpmao_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', 'wpmao_sanitize_user', 10, 3);