简体   繁体   English

如何使用 Wordpress rest API 创建新类别

[英]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会导致相同的错误。

After much fiddling, I was finally able to make it work by changing the user role to Editor or Administrator on the WordPress admin dashboard.经过一番折腾,我终于能够通过在 WordPress 管理仪表板上将用户角色更改为编辑器或管理员来使其工作。 I hope this will help someone else who is struggling with the same issue.我希望这将帮助其他在同一问题上苦苦挣扎的人。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用其REST API在另一个wordpress网站中创建wordpress发布? - How can I create a wordpress post in another wordpress site using their REST API? How to create a new rest url prefix in Wordpress REST API? - How to create a new rest url prefix in Wordpress REST API? 在 wordpress 中,我可以在我的主题中使用 PHP 添加一个新类别吗 - In wordpress Can I add a new Category using PHP in my theme 如何使用rest-api添加类别来创建帖子 - How to add category to create post using rest-api 我如何使用WordPress REST API从我的角度应用程序中提取更多WordPress帖子 - How can I pull in more WordPress posts from my angular app, using the WordPress REST api 如何在Wordpress中使用脚本创建类别的子类别? - how we can create subcategories of a category using script in wordpress? 创建新的magento Rest api以获取magento中的类别列表 - Create new magento Rest api to get category list in magento 我如何使用类别ID获取Wordpress帖子? - How i can get wordpress post using category id? 如何使用WordPress REST API返回某个月的所有帖子 - How can I return all posts from a certain month using the WordPress REST api 如何为 ZC31B32364CE19CA8ZFCD150A417ECCE5 应用程序创建 WordPress REST API - How to create a WordPress REST API for an android application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM