对于强大而自由的wordpress来说,可能本身功能并不强大,但是他却有一个强大的接口系统!让我们能够发挥自己的各种想象力去完善。官方之所以不做那么多,归根结底还是每个用户使用习惯不一样,需求不一样。因为这这只是一个博客系统!
甭说,既然有童鞋在寻找让wordpress支持中文用用户名注册的办法,也不难。搜索了一大堆,解决办法多种多样!这里带给大家的是来自V7V3基于露兜代码进行安全优化的一段增强代码。其实让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);
以上代码来自露兜博客~V7V3借鉴了wp-includes/formatting.php中sanitize_user函数的写法,对数据做了进一步的安全验证~~
原作者:V7V3