[英]How can I create a new category using Wordpress rest API
I've searched but I cannot seem to find this one specific answer.我已经搜索过,但似乎找不到这个特定的答案。 I can successfully create new tags using the below code:我可以使用以下代码成功创建新标签:
<?php
function wpAutoPoster($token,$endpointUrl,$data){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $endpointUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'accept: application/json',
'Authorization: Bearer '.$token
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
$jwtAuthToken="xxx";
$endpointUrl="https://www.example.com/wp-json/wp/v2/tags";
$data=json_encode(['name'=>'my new tag 1']);
$response=json_decode(wpAutoPoster($jwtAuthToken,$endpointUrl,$data));
var_dump($response);
The above works as expected.以上按预期工作。
Now, to create categories, I just change the following 2 lines:现在,要创建类别,我只需更改以下 2 行:
$endpointUrl="https://www.example.com/wp-json/wp/v2/categories";
$data=json_encode(['name'=>'my new category 1'])
And this is the response I get:这是我得到的回应:
object(stdClass)#1 (3) { ["code"]=> string(18) "rest_cannot_create" ["message"]=> string(60) "Sorry, you are not allowed to create terms in this taxonomy." object(stdClass)#1 (3) { ["code"]=> string(18) "rest_cannot_create" ["message"]=> string(60) "抱歉,您不能在此分类中创建术语。" ["data"]=> object(stdClass)#2 (1) { ["status"]=> int(403) } } ["data"]=> object(stdClass)#2 (1) { ["status"]=> int(403) } }
This goes out for my Custom Post Types as well.这也适用于我的自定义帖子类型。
Setting 'hierarchical' => true results in the same error.设置'hierarchical' => true会导致相同的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.