解决WordPress支持使用中文用户名注册和登录的方法

解决WordPress支持使用中文用户名注册和登录的方法

资源外星人导读:本站为大家带来解决WordPress支持使用中文用户名注册和登录的方法文章,更多建站技术,请继续关注资源外星人!

我们在使用WordPress建站的过程当中,有些客户要求后台可以使用中文用户名来进行注册和登录。借鉴wp-includes/formatting.php中sanitize_user函数的写法,可以使用下面的方法实现。

切换到模板目录,打开 functions.php 文件,添加如下代码:

functions.php

<?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);
?>

以上就是资源外星人整理的解决WordPress支持使用中文用户名注册和登录的方法全部内容,希望对大家有所帮助!