之前有不少小夥伴詢問小編關於 WordPress 網站註冊不支持中文用户名的問題,之前小編一直沒有時間來做個相關的 WordPress 教程其實這類問題百度谷歌一下也是有很多解決方案的趁着今天小編有點空閒立馬寫個教程記錄下,也便於其他新人查看~~其實讓 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-zp{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 中 sanitize_user 函數的寫法,對數據做了進一步的安全驗證~~