![](/img/trans.png)
[英]Rewrite wordpress sanitize_user function to disallow whitespace for username registration form
[英]How to modify sanitize_user WordPress function to accept user name in cyrillic
原裝 function:
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 測試正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.