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就可以支持中文用户名注册和登入了非常简单。

如果嫌麻烦也可以使用wordpress中文用户名插件:chinese-username 

相关文章

我想将wordpress的默认接口路由改掉,愿意是默认的带一个 wp...
wordpress自定义分类法之后,我看到链接都自动在后面添加了一...
事情是这样的,我用 get_post_type 函数创建了一个自定义分类...
最近网站莫名其妙的被顶上了,过一个多小时,就注册一个账号...
最近服务器要到期了,就想着把网站转移到另外一台服务器,本...
今天在写wordpress的接口,然后碰到个奇怪的问题,怎么访问都...