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號又微信號)右上方掃一掃可加博主微信
所寫所說,是心之所感,思之所悟,行之所得;文當無敷衍,落筆求簡潔。 以所舍,求所獲;有所依,方所成!