我们在使用WordPress建站的过程当中,有些客户要求后台可以使用中文用户名来进行注册和登录。借鉴wp-includes/formatting.php中sanitize_user函数的写法,可以使用下面的方法实现。
切换到模板目录,打开 functions.php 文件,添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php function 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', 'sanitize_user', 10, 3); ?> |