如何以编程方式在希伯来语中插入希伯来语用户,用户名?

问题描述

我正在尝试从我的自定义网站(这是另一种语言)插入用户。用户以英语或希伯来语在该网站上注册。

使用功能wp_insert_user

$usernicename = $userpost['first_name'] . $userpost['last_name'];
    $user_login_name = $userpost['last_name'].$userpost['first_name'];
    $user_password = "xyz@123";

    $userdata = array(
    'user_pass'             => $userpost['password'],'user_login'            => $user_login_name,'user_nicename'         => $usernicename,'user_email'            => $userpost['email'],'first_name'            => $userpost['first_name'],'last_name'             => $userpost['last_name'],'user_registered'       => $userpost['registration'],'role'                  => 'subscriber',);
$id = wp_insert_user( $userdata ) ;

所有名称为英语的用户都可以成功插入,但是使用希伯来语/其他语言的用户会出现错误

object(WP_Error)#11230 (2) {
  ["errors"]=>
  array(1) {
    ["empty_user_login"]=>
    array(1) {
      [0]=>
      string(46) "Cannot create a user with an empty login name."
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

我已通过管理员设置更改了网站语言,但这还是行不通的。

解决方法

我已经找到解决问题的方法,主要是因为用户名的清理。 希伯来语字符被santizied,并返回null,这就是为什么它会给出错误的空登录名。

使用我发现的代码或wordpress插件可以解决此问题

1。wordpress special character 2。Hebrew Username

或者您可以使用下面的代码并将其粘贴到您的function.php文件中,它将覆盖用户名的Santization函数。

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

//Overrides the Wordpress sanitize_user filter to allow hebrew letters and english letters only

function hu_sanitize_user ($username,$raw_username,$strict)
{
//Strip HTML Tags
$username = wp_strip_all_tags ($raw_username);

//Remove Accents
$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){
    //Replace
    $username = preg_replace( '[\p{Hebrew}a-zA-Z]',$username );
}

//Remove Whitespaces
$username = trim ($username);

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

return $username;
}

上述两种解决方案(代码/插件)都只能在用户名中输入8个希伯来字符。这是因为当user_login列声明为varchar(60)时,与英语字符相比,希伯来字符占用了更多字节,因此您必须根据需要进行更改。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...