重写wordpress-splice_user函数,以禁止用户名注册表单使用空格



在wordpress中,sance_user((函数允许您定义应用于不同表单的限制规则。我在这里感兴趣的是注册表格,我正在尝试个性化这个表格。我使用了Wordpress主题作为我的登录插件,一切都很好。但是,在注册过程中,我希望防止用户输入带有空格的用户名。默认情况下,sance_user((函数允许使用带有空格的用户名(例如:John Doe(,但我不想要它,我只想让他们写没有空格的用户名

这是defaut-sance_user((函数:

function sanitize_user( $username, $strict = false ) {
$raw_username = $username;
$username     = wp_strip_all_tags( $username );
$username     = remove_accents( $username );
// Kill octets.
$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
// Kill entities.
$username = preg_replace( '/&.+?;/', '', $username );
// If strict, reduce to ASCII for max portability.
if ( $strict ) {
$username = preg_replace( '|[^a-z0-9 _.-@]|i', '', $username );
}
$username = trim( $username );
// Consolidate contiguous whitespace.
$username = preg_replace( '|s+|', ' ', $username );
/**
* Filters a sanitized username string.
*
* @since 2.0.1
*
* @param string $username     Sanitized username.
* @param string $raw_username The username prior to sanitization.
* @param bool   $strict       Whether to limit the sanitization to specific characters.
*/
return apply_filters( 'sanitize_user', $username, $raw_username, $strict );

}

我试图将函数传递给strict,但没有结果(function sanitize_user( $username, $strict = true ) {...}

有人能帮我吗?

试试这个

add_filter( 'sanitize_user', 'remove_whitespace_from_username', 10, 3 );
function remove_whitespace_from_username( $username, $raw_username, $strict  ){
$username = preg_replace('/s+/', ' ', $username);
return $username;
}

您也可以使用以下代码来删除空白

您也可以使用str_replace((:-

$username = str_replce(' ','',$username); 

最新更新