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
Copy file name to clipboardExpand all lines: extending/modifying/index.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,13 @@ title: Modifying Responses
3
3
---
4
4
The default endpoints of the WordPress REST API are designed to be sensible defaults in terms of what data is returned. This fits the 80/20 rule by providing for 80% of sites and uses, but these default collections of data cannot always fulfill the needs of every one of the millions of sites.
5
5
6
-
The REST API is designed to be highly extensible, like the rest (no pun intended) of WordPress. This document details how to add additional data, including but not limited to post or user meta data to the responses of default endpoints, using the function `register_api_field`; a helper function designed to add fields to the response for a specific option.
6
+
The REST API is designed to be highly extensible, like the rest (no pun intended) of WordPress. This document details how to add additional data, including but not limited to post or user meta data to the responses of default endpoints, using the function `register_rest_field`; a helper function designed to add fields to the response for a specific option.
7
7
8
-
`register_api_field` provides a single way for modifying all responses for an object. For example, if it is used to add a field to the posts object, this field will be included in all responses, whether they are for single or multiple items.
8
+
`register_rest_field` provides a single way for modifying all responses for an object. For example, if it is used to add a field to the posts object, this field will be included in all responses, whether they are for single or multiple items.
9
9
10
10
In addition, it allows you to update that value from the endpoints the field is registered on.
11
11
12
-
It is important to note that in the context of this document, the term "field" refers to a field in the object returned by the API. It does not refer to a meta field of a post, comment or user. While it `register_api_field` can be used to add meta fields to a response, it can be used to add any data.
12
+
It is important to note that in the context of this document, the term "field" refers to a field in the object returned by the API. It does not refer to a meta field of a post, comment or user. While it `register_rest_field` can be used to add meta fields to a response, it can be used to add any data.
13
13
14
14
15
15
Important Note about Changing Responses
@@ -24,18 +24,18 @@ Adding fields is not dangerous, so if you need to modify data, it's much better
24
24
Note that the API cannot prevent you from changing responses, but the code is structured to strongly discourage this. Internally, field registration is powered by filters, and these can be used if you absolutely have no other choice.
25
25
26
26
27
-
What `register_api_field` Does
27
+
What `register_rest_field` Does
28
28
------------------------------
29
29
30
-
In the infrastructure for responses, the global variable $wp_rest_additional_fields, is used for holding the fields, by object name to be added to the responses to those objects. The REST API provides `register_api_field` as a utility function for adding to this global variable. Adding to it directly should be avoided to ensure maximum forward-compatibility.
30
+
In the infrastructure for responses, the global variable $wp_rest_additional_fields, is used for holding the fields, by object name to be added to the responses to those objects. The REST API provides `register_rest_field` as a utility function for adding to this global variable. Adding to it directly should be avoided to ensure maximum forward-compatibility.
31
31
32
32
For each object -- a post type, or users, terms, comments, meta, etc, $wp_rest_additional_fields contains an array of fields, each of which can have a value for callbacks used to retrieve the value, update the value using any endpoint that field is added to that can be used for updating.
33
33
34
34
35
-
How To Use `register_api_field`
35
+
How To Use `register_rest_field`
36
36
-------------------------------
37
37
38
-
The function `register_api_field` field accepts three parameters:
38
+
The function `register_rest_field` field accepts three parameters:
39
39
40
40
1.`$object_type`: The name of the object, as a string, or an array of the names of objects the field is being registered to. When adding to posts type endpoints, the name of the post type(s) should be used. Alternatively "terms", "meta", "user" or "comments" may be used.
0 commit comments