【问题标题】:WordPress REST API Update User base on usernameWordPress REST API 更新用户基于用户名
【发布时间】:2018-01-24 01:37:10
【问题描述】:

有没有一种方法可以根据用户名而不是 ID 来更新用户库?当我尝试创建用户时,将在另一个网站上添加,但创建的 ID 与我的主网站上的不同。现在我的问题是根据用户名更新该用户库。有可能吗?

这是我尝试更新的内容,它已经可以使用,但基于 ID。

更新的验证码

function update_user_api( $entry, $form ) {

$firstname = $entry[3];
$lastname = $entry[4];
$email = $entry[5];

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://mywebsite/wp-json/wp/v2/users/11?context=edit", //I would like to change this to username if possible
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "first_name=".$firstname."&last_name=".$lastname."",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic MDAwOjAwMA==",
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded",
    "postman-token: 0a02f782-eab1-e4d7-e866-737c6894fad3"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
}
add_action( 'gform_after_submission_16', 'update_user_api', 10, 2 );

【问题讨论】:

    标签: wordpress wordpress-rest-api


    【解决方案1】:

    我们也遇到了这个问题,我们的解决方法是将另一个网站创建的用户 ID 保存为主网站上用户的用户元数据,并使用它通过 wp rest api 进行更新。希望能帮助到你。干杯

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多