如何修改 sanitize_user WordPress 函数以接受西里尔字母的用户名

问题描述

函数

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

我想修改它以在用户执行注册时接受西里尔文(塞尔维亚语)的用户名

塞尔维亚西里尔字母 абвгдђежзијклљмнњопрстћуфхцчџшАБВГДЂЕЖЗИЈКЛЉМНЊОПРСТЋУФХЦЧЏШ

解决方法

function mujuonly_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
    // If strict,reduce to ASCII and Cyrillic characters for maximum portability.
    if ( $strict )
        $username = preg_replace( '|[^a-zа-я0-9 _.\-@]|iu',$username );

    $username = trim( $username );

    // Consolidate contiguous whitespace
    $username = preg_replace( '|\s+|',' ',$username );

    return $username;
}

add_filter( 'sanitize_user','mujuonly_sanitize_user',10,3 );

试试这个 - 使用 WP 5.6 测试正常