wordpress如何支持中文名注册来访者账号
[重要通告]如您遇疑难杂症,本站支持知识付费业务,扫右边二维码加博主微信,可节省您宝贵时间哦!
我们在使用wordpress的时候,都知道注册的时候只能是英文或者数字之类,不可以使用中文,但是别名可以,不过有些时候我们注册的时候就想让来访者直接注册为中文,那操作方式如下:
直接粘贴下面代码插入到functions.php即可实现注册为中文账号;
//支持中文名注册
function git_sanitize_user ($username, $raw_username, $strict) {
$username = wp_strip_all_tags( $raw_username );
$username = remove_accents( $username );
$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
$username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
if ($strict) {
$username = preg_replace ('|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username);
}
$username = trim( $username );
$username = preg_replace( '|\s+|', ' ', $username );
return $username;
}
add_filter ('sanitize_user', 'git_sanitize_user', 10, 3);
function git_comment_add_at( $comment_text, $comment = '') {
if( $comment->comment_parent > 0) {
$comment_text = '@<a href="#comment-' . $comment->comment_parent . '">'.get_comment_author( $comment->comment_parent ) . '</a> ' . $comment_text;
}
return $comment_text;
}
add_filter( 'comment_text' , 'git_comment_add_at', 20, 2);
方法二:
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);
方法三
打开 wp-includes/formatting.php,找到
function sanitize_user( $username, $strict = false ) {
在这句函数的下一行添加一句下面的代码
$strict = false;
PS:每次WordPress升级的之后,还需要做同样的处理方式;
问题未解决?付费解决问题加Q或微信 2589053300 (即Q号又微信号)右上方扫一扫可加博主微信
所写所说,是心之所感,思之所悟,行之所得;文当无敷衍,落笔求简洁。 以所舍,求所获;有所依,方所成!