最新消息:本站所有跳转向bbs.ykit.cn的附件将全面停止,附件已转移到https://www.qingsj.com

WordPress 如何使用中文用户名登录

wordpress教程 有客 782浏览

一直使用的是 WordPress 的程序,咱们使用的是中文,在 WordPress 中如何使用中文用户名,这里涉及到一个编码问题,我使用的 utf-8 的编码,理论上是支持中文和英文的字符,可是原生的 WordPress 程序是不支持中文编码的,不过有方法可以实现。

在主题的 functions.php 文件中加入一段代码即可让 WordPress 支持中文用户名。

function fanly_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 );
    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', 'fanly_sanitize_user', 10, 3);

默认情况下,WordPress 认为中文都是非法字符,所以他会在用户注册的时候直接显示用户名不合法,但是我们可以修改这个过滤的规则,让中文通过验证即可。

转载请注明:有客帮 » WordPress 如何使用中文用户名登录