You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example shows how to allow reading and writing of a post meta field. This will allow that field to be updated via a POST request to `wp-json/wp/v2/posts/<post-id>` or created along with a post via a POST request to `wp-json/wp/v2/posts/`.
129
129
130
130
Note that for meta fields registered on custom post types, the post type must have `custom-fields` support. Otherwise the meta fields will not appear in the REST API.
131
+
132
+
133
+
## Adding Links to the API Response
134
+
WordPress generates a list of links associated with the queried resource to make it easier to navigate to related resources. The `embeddable` attribute controls whether the linked resource appears in the `_embedded` section of the repsonse.
_A sample of links from a Make.WordPress.org post_
177
+
178
+
Custom links can be added to the response by using the `WP_REST_Response::add_link()` method. This method accepts three parameters, the link relation, the URL and optionally a list of link attributes. For example, to add the `author` and `wp:term` link.
The link relation MUST be either a [registered link relation from the IANA](https://www.iana.org/assignments/link-relations/link-relations.xhtml) or a URL that is under your control.
192
+
193
+
`author` is a registered link relation described as "the context's author", we use that to point to the WordPress user who wrote the post. No link relation exists that describes the terms associated with a post, so WordPress uses the `https://api.w.org/term` URL. This is transformed to `wp:term` when generating the response by using a CURIE.
194
+
195
+
### Registering a CURIE
196
+
197
+
WordPress version 4.5 introduced support for Compact URIs, or CURIEs. This makes it possible to reference links by a much simpler identifier than the full URL which could easily be quite lengthy.
198
+
199
+
A CURIE is registered using the `rest_response_link_curies` filter. For example.
200
+
201
+
```php
202
+
<?php
203
+
function my_plugin_prefix_register_curie( $curies ) {
204
+
205
+
$curies[] = array(
206
+
'name' => 'my_plugin',
207
+
'href' => 'https://api.mypluginurl.com/{rel}',
208
+
'templated' => true,
209
+
);
210
+
211
+
return $curies;
212
+
}
213
+
```
214
+
215
+
This will convert link URLs from `https://api.mypluginurl.com/my_link` to `my_plugin:my_link` in the API response. The full URL must still be used when adding links using `WP_REST_Response::add_link`.
0 commit comments