Skip to content

Commit c7fa82c

Browse files
Document the new Resource Discovery feature in WordPress 5.5.
1 parent 03acf4a commit c7fa82c

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

extending-the-rest-api/adding-custom-endpoints.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ add_action( 'rest_api_init', function () {
257257
} );
258258
```
259259

260+
### Discovery
261+
262+
If you'd like to enable [Resource Discovery](https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/#resource-discovery) for your custom endpoint, you can do so using the `rest_queried_resource_route` filter. For example, consider a [custom query var](https://developer.wordpress.org/reference/hooks/query_vars/) `my-route` that contains the id of your custom resource. The following code snippet would add a discovery link whenever the `my-route` query var is used.
263+
264+
```php
265+
function my_plugin_rest_queried_resource_route( $route ) {
266+
$id = get_query_var( 'my-route' );
267+
if ( ! $route && $id ) {
268+
$route = '/my-ns/v1/items/' . $id;
269+
}
270+
271+
return $route;
272+
}
273+
add_filter( 'rest_queried_resource_route', 'my_plugin_rest_queried_resource_route' );
274+
```
275+
276+
Note: If your endpoint is describing a custom post type or custom taxonomy you will most likely want to be using the `rest_route_for_post` or `rest_route_for_term` filters instead.
277+
260278
## The Controller Pattern
261279

262280
The controller pattern is a best practice for working with complex endpoints with the API.

extending-the-rest-api/adding-rest-api-support-for-custom-content-types.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ function my_book_cpt() {
7878
}
7979
```
8080

81+
If you are using a custom `rest_controller_class`, then the REST API is unable to automatically determine the route for a given post. In this case, you can use the `rest_route_for_post` filter to provide this information. This allows for your custom post type to be properly formatted in the Search endpoint and enables automated discovery links.
82+
83+
```php
84+
function my_plugin_rest_route_for_post( $route, $post ) {
85+
if ( $post->post_type === 'book' ) {
86+
$route = '/wp/v2/books/' . $post->ID;
87+
}
88+
89+
return $route;
90+
}
91+
add_filter( 'rest_route_for_post', 'my_plugin_rest_route_for_post', 10, 2 );
92+
```
8193

8294
## Registering A Custom Taxonomy With REST API Support
8395

@@ -127,6 +139,19 @@ function my_book_taxonomy() {
127139
}
128140
```
129141

142+
If you are using a custom `rest_controller_class`, then the REST API is unable to automatically determine the route for a given term. In this case, you can use the `rest_route_for_term` filter to provide this information. This allows for your custom taxonomy to be properly formatted in the Search endpoint and enables automated discovery links.
143+
144+
```php
145+
function my_plugin_rest_route_for_term( $route, $term ) {
146+
if ( $term->taxonomy === 'genre' ) {
147+
$route = '/wp/v2/genre/' . $term->term_id;
148+
}
149+
150+
return $route;
151+
}
152+
add_filter( 'rest_route_for_term', 'my_plugin_rest_route_for_term', 10, 2 );
153+
```
154+
130155
## Adding REST API Support To Existing Content Types
131156

132157
If you need to add REST API support for a custom post type or custom taxonomy you do not control, for example a theme or plugin you are using, you can use the `register_post_type_args` filter hook that exists since WordPress version 4.6.0.

using-the-rest-api/discovery.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,14 @@ This would add the `testplugin/v1` namespace to the index:
153153
}
154154
```
155155

156+
## Resource Discovery
157+
158+
As of [WordPress 5.5](https://core.trac.wordpress.org/changeset/48273) the REST API also provides a discovery mechanism for identifying the REST API route equivalent of the current document. A link is added with a `rel` of `alternate` and a `type` of `alternate/json` that points to a REST API url. The link is added both as a [`Link` header](#link-header) and a [`<link>` element](#element).
159+
160+
For instance, in the `<head>` section of this page, the following `<link>` appears.
161+
162+
```html
163+
<link rel="alternate" type="application/json" href="https://developer.wordpress.org/wp-json/wp/v2/rest-api-handbook/23085">
164+
```
165+
166+
Links are added for post, pages, and other custom post types, as well as terms and author pages. Links are not currently output for post archives or search results.

0 commit comments

Comments
 (0)