I am trying to create users in wordpress Version 4.9.1, remotely using the wordpress API. This is my PHP code below:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8080",
CURLOPT_URL => "http://localhost:8080/my_site/wp-json/wp/v2/users/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent- Disposition: form-data; name=\"username\"\r\n\r\nwakawaka\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\[email protected]\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\ncITt xOSx HGKG GjTw No33 hzpG\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ACCESS_TOKEN\"\r\n\r\ncITt xOSx HGKG GjTw No33 hzpG\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 84cad22c-9f08-d2b1-18f6-6eb9880f3f5f"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
When i execute the code, create new user fails and this is error code I get:
{
"code":"rest_cannot_create_user",
"message":"Sorry, you are not allowed to create new users.",
"data": {"status":401}
}
How can I securely and remotely Create new user in wordpress using wordpress Rest API?
1 Answer
To create any content on a WordPress website via REST-API your must use authentication.
You have two options for getting OAuth tokens, Detailed explanations in the following link: