Reputation: 53
I find the documentation for the new WordPress REST API (v2) very inadequate and/or incomplete. I have a plugin which defines custom post types and I would like to use the REST API for its intended purpose. I've tried to follow the example in the documentation, but when I try the relevant URL (http://example.com/wp-json/wp/v2/prsp/v1/attributes) I get a "rest_no_route" response.
My custom post types do show up on the response for "http://example.com/wp-json/wp/v2/types" however.
Can anyone comment?
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => 'prsp-top-level-handle',
'rewrite' => array('slug' => 'prsp-attribute', 'with_front' => FALSE),
'has_archive' => false,
'hierarchical' => false,
...
'show_in_rest' => true,
'rest_controller_class' => 'WP_REST_Posts_Controller'
);
register_post_type('prsp-attribute', $args);
...
$this->admin = new ProspectAdmin($this->get_version());
$this->loader->add_action('admin_init', $this->admin, 'do_prsp_init', null, null);
$this->loader->add_action('admin_menu', $this->admin, 'add_prsp_menus', null, null);
$this->loader->add_action('rest_api_init', $this->admin, 'add_rest_api', null, null);
...
public function rest_get_attributes()
{
return 'rest_get_attributes()'; // Temporary test for success
} // rest_get_attributes()
public function add_rest_api()
{
register_rest_route('prsp/v1', '/attributes', array(
'methods' => 'GET',
'callback' => array($this, 'rest_get_attributes')
));
register_rest_route('prsp/v1', '/attribute/(?P<id>\w+)', array(
'methods' => 'GET',
'callback' => array($this, 'rest_get_attribute')
));
} // add_rest_api()
And, where is the explanation of the format for the second parameter of register_rest_route()?
Upvotes: 0
Views: 2099
Reputation: 2820
The URL should be http://example.com/wp-json/prsp/v1/attributes
.
You may list the existing routes and endpoints via GET http://example.com/wp-json
(or GET http://example.com/wp-json/prsp/v1
).
Upvotes: 2