A-A+

纯代码实现 wordpress 支持中文注册的方法

2015年08月25日 PHP开源系统 暂无评论 阅读 13 views 次

之前在夏日博客的文章中有提到过,就是 wordpress 不支持中文用户的登陆与注册,今天再来介绍一种更简捷的方法,就是直接使用纯代码来进行实现,我们还是先打开万能的主题下 functions.php 文件吧,在这个文件中加入如下的代码:

  1. function ludou_sanitize_user ($username$raw_username$strict) {  
  2.   $username = wp_strip_all_tags( $raw_username );  
  3.   $username = remove_accents( $username );  
  4.   // Kill octets  
  5.   $username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', ''$username );  
  6.   $username = preg_replace( '/&.+?;/', ''$username ); // Kill entities  
  7.   
  8.   // 网上很多教程都是直接将$strict赋值false,  
  9.   // 这样会绕过字符串检查,留下隐患  
  10.   if ($strict) {  
  11.     $username = preg_replace ('|[^a-z\p{Han}0-9 _.\-@]|iu', ''$username);  
  12.   }  
  13.   
  14.   $username = trim( $username );  
  15.   // Consolidate contiguous whitespace  
  16.   $username = preg_replace( '|\s+|', ' ', $username );  
  17.   
  18.   return $username;  
  19. }  
  20.   
  21. add_filter ('sanitize_user', 'ludou_sanitize_user', 10, 3);  

好了,现在再来尝试一下,看看你的 wordpress 是否已经支持中文用户名注册和登录了。

标签:

给我留言