A-A+

wordpress用户注册不支持中文

2014年12月24日 PHP开源系统 暂无评论 阅读 24 views 次

在用户注册中,其实没有哪个网站有中文用户名注册的习惯,通常都是使用英文字母之类的,在WP中也是一样的不支持中文用户名的注册,如果想要使用中文用户名,其实网上提供了许多方法,这里找了个靠谱的函数,在 wordpress 主题的函数文件 function 中加入如下的方法代码即可:

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])|', '', $use
rname );
  $username = preg_replace( '/&.+?;/', '', $username ); // Kill en
tities
   
  // 网上很多教程都是直接将$strict赋值false,
  // 这样会绕过字符串检查,留下隐患
  if ($strict) {
    $username = preg_replace ('|[^a-z\p{Han}0-9 _.\-@]|iu', '', $us
ername);
  }
   
  $username = trim( $username );
  // Consolidate contiguous whitespace
  $username = preg_replace( '|\s+|', ' ', $username );
   
  return $username;
}
   
add_filter ('sanitize_user', 'ludou_sanitize_user', 10, 3);

这段代码借鉴了wp-includes/formatting.php中sanitize_user函数的写法,对数据做了进一步的安全验证~~

标签:

给我留言