Xampp + Wordpress Rest API(401 代码:rest_cannot_create)
Xampp + Wordpress Rest API (401 code: rest_cannot_create)
我想达到什么目标?
- 我想用 wordpress rest api
为我的自定义 post 类型创建一个 post
哪个 Api 路由有问题?
- http://localhost/proftaak/wp-json/wp/v2/mycustomposttype
您使用的是什么 http 方法?
- POST 方法(post = 不工作,get = 工作)
您使用的是什么本地 apache 服务器?
- 我正在使用 xampp 作为本地 apache + mysql 服务器。
我做了什么?
- 我创建了一个新的自定义 post 类型。看这里:
add_action('init', 'create_custom_post_types');
add_action('admin_menu', 'remove_default_post_types');
function create_custom_post_types()
{
// Apparaten (requests)
register_post_type('apparaten', [
'public' => true,
'show_in_rest' => true,
'label' => 'Apparaten',
'menu_icon' => 'dashicons-smartphone',
'capabilities' => array(
'create_posts' => false
),
'labels' => [
'singular_name' => 'Apparaat'
],
]);
}
function remove_default_post_types()
{
remove_menu_page( 'edit.php' );
remove_menu_page( 'edit-comments.php' );
}
我已将我的自定义 post 类型设置为 show_in_rest 为真,因为我想使用它。
但是,如果我在 postman 中对 url 执行 post,我会得到以下信息:
我研究了很多但找不到解决方案。
有人有想法吗?
我找到了解决方案
对于自定义 post 类型,您需要创建自己的 rest api 路由(wp rest api 仅添加 GET 请求)。
您可以使用 wordpress 中的 "register_rest_route" 函数来做到这一点。
我想达到什么目标?
- 我想用 wordpress rest api
为我的自定义 post 类型创建一个 post
哪个 Api 路由有问题?
- http://localhost/proftaak/wp-json/wp/v2/mycustomposttype
您使用的是什么 http 方法?
- POST 方法(post = 不工作,get = 工作)
您使用的是什么本地 apache 服务器?
- 我正在使用 xampp 作为本地 apache + mysql 服务器。
我做了什么?
- 我创建了一个新的自定义 post 类型。看这里:
add_action('init', 'create_custom_post_types');
add_action('admin_menu', 'remove_default_post_types');
function create_custom_post_types()
{
// Apparaten (requests)
register_post_type('apparaten', [
'public' => true,
'show_in_rest' => true,
'label' => 'Apparaten',
'menu_icon' => 'dashicons-smartphone',
'capabilities' => array(
'create_posts' => false
),
'labels' => [
'singular_name' => 'Apparaat'
],
]);
}
function remove_default_post_types()
{
remove_menu_page( 'edit.php' );
remove_menu_page( 'edit-comments.php' );
}
我已将我的自定义 post 类型设置为 show_in_rest 为真,因为我想使用它。
但是,如果我在 postman 中对 url 执行 post,我会得到以下信息:
我研究了很多但找不到解决方案。
有人有想法吗?
我找到了解决方案
对于自定义 post 类型,您需要创建自己的 rest api 路由(wp rest api 仅添加 GET 请求)。
您可以使用 wordpress 中的 "register_rest_route" 函数来做到这一点。