让wordpress支持用户名使用中文
打开文件 【网站根目录/wp-includes/formatting.php】,查找到sanitize_user函数,将其中的
2106-2108行:
if ( $strict ) {
$username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );
}
修改为:
if ( $strict ) {
$username = preg_replace( '|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username );
}
if ( $strict ) {
$username = preg_replace( '|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username );
}
以后每次升级wordpress都要设置一下。
还有一个办法,是在主题下的functions.php 最后一行,添加如下代码,我用的此方法。
//让wordpress支持用户名使用中文,支持9个汉字
function git_sanitize_user ($username, $raw_username, $strict) {
$username = wp_strip_all_tags( $raw_username );
$username = remove_accents( $username );
$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
$username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
if ($strict) {
$username = preg_replace ('|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username);
}
$username = trim( $username );
$username = preg_replace( '|\s+|', ' ', $username );
return $username;
}
add_filter ('sanitize_user', 'git_sanitize_user', 18, 3);