好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

wordpress支持中文用户名注册的2种实现方法 - Wor

wordpress支持中文用户名注册的2种实现方法

wordpress是不支持中文名字注册的如果你要增加中文注册是需要在注册判断或登录处进行二次开发了,具体如下.

之前写了一篇,代码如下:

function  ludou_non_strict_login(  $username ,  $raw_username ,  $strict  ) {       if ( ! $strict  )           return   $username ;       return  sanitize_user( stripslashes ( $raw_username ), false);  }  add_filter( 'sanitize_user' ,  'ludou_non_strict_login' , 10, 3); 

现在回过头看了一下,这篇教程对用户名的过滤太少,容易出现安全问题,今天介绍新的方法,借鉴了wp-includes/formatting.php中sanitize_user函数的写法,同样是将以下php代码复制到当前主题目录下的functions.php中,即可让WordPress支持使用中文用户名注册和登录,代码如下:

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 ); //开源软件:phpfensi测试数据     }     $username  = trim(  $username  );     // Consolidate contiguous whitespace      $username  = preg_replace(  '|\s+|' ,  ' ' ,  $username  );     return   $username ;  }  add_filter ( 'sanitize_user' ,  'ludou_sanitize_user' , 10, 3); 

增加到function.php文件中之后你就可以在wordpress注册中加中文名字了.

查看更多关于wordpress支持中文用户名注册的2种实现方法 - Wor的详细内容...

  阅读:68次