A-A+
纯代码实现 wordpress 支持中文注册的方法
之前在夏日博客的文章中有提到过,就是 wordpress 不支持中文用户的登陆与注册,今天再来介绍一种更简捷的方法,就是直接使用纯代码来进行实现,我们还是先打开万能的主题下 functions.php 文件吧,在这个文件中加入如下的代码:
- 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);
好了,现在再来尝试一下,看看你的 wordpress 是否已经支持中文用户名注册和登录了。