$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost/wordpress/wp-json/wp/v2/users/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"jane\",\"password\":\"secret\",\"email\":\"jane@example.com\"}");
curl_setopt($ch, CURLOPT_USERPWD, 'restapi_newuser' . ':' . 'ZAFA nAe4 uXM7 XN5c Dq8g CSpw');

$headers = array();
$headers[] = 'Content-Type: application/json';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

echo $result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);

message i get is

{"code":"rest_cannot_create_user","message":"Sorry, you are not allowed to create new users.","data":{"status":401}}

I am using XAMPP / wp 6.0.2 / Application Passwords (under the user section)

this is a standalone script that just needs to create a new user it need not check if a user exists etc etc. I feel that i am missing out on something ? Any ideas what i am doing wrong ?

  • Does restapi_newuser have the create_users capability (or a role which does - by default, only the Administrator/Super Admin roles)?
    – bosco
    Sep 19 at 14:16


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy