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/adding/index.md
+5-2
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ add_action( 'rest_api_init', function () {
49
49
} );
50
50
```
51
51
52
-
Right now, we're only registering the one endpoint for the route. ("Route" is the URL, whereas "endpoint" is the function behind it that corresponds to a method *and* a URL. For more, see the [Glossary](/glossary.html).) Each route can have any number of endpoints, and for each endpoint, you can define the HTTP methods allowed, a callback function for responding to the request and a permissions callback for creating custom permissions. In addition you can define allowed fields in the request and for each field specify a default value, a sanitization callback, a validation callback, and whether the field is required.
52
+
Right now, we're only registering the one endpoint for the route. ("Route" is the URL, whereas "endpoint" is the function behind it that corresponds to a method *and* a URL. For more, see the [Glossary](/glossary.html).) If your site domain is `example.com` and you've kept the API path of `wp-json`, then the full URL would be `http://example.com/wp-json/myplugin/v1/author/(?P<id>\d+)`. Each route can have any number of endpoints, and for each endpoint, you can define the HTTP methods allowed, a callback function for responding to the request and a permissions callback for creating custom permissions. In addition you can define allowed fields in the request and for each field specify a default value, a sanitization callback, a validation callback, and whether the field is required.
53
53
54
54
55
55
### Namespacing
@@ -99,6 +99,7 @@ function my_awesome_func( WP_REST_Request $request ) {
99
99
$parameters = $request->get_url_params();
100
100
$parameters = $request->get_query_params();
101
101
$parameters = $request->get_body_params();
102
+
$parameters = $request->get_json_params();
102
103
$parameters = $request->get_default_params();
103
104
104
105
// Uploads aren't merged in, but can be accessed separately:
@@ -110,6 +111,8 @@ function my_awesome_func( WP_REST_Request $request ) {
110
111
111
112
Normally, you'll get every parameter brought in unaltered. However, you can register your arguments when registering your route, which allows you to run sanitization and validation on these.
112
113
114
+
If the request has the `Content-type: application/json` header set and valid JSON in the body, `get_json_params()` will return the parsed JSON body as an associative array.
115
+
113
116
Arguments are defined as a map in the key `args` for each endpoint (next to your `callback` option). This map uses the name of the argument of the key, with the value being a map of options for that argument. This array can contain a key for `default`, `required`, `sanitize_callback` and `validate_callback`.
114
117
115
118
*`default`: Used as the default value for the argument, if none is supplied.
@@ -240,7 +243,7 @@ The Controller Pattern
240
243
241
244
The controller pattern is a best practice for working with complex endpoints with the API.
242
245
243
-
It is recommended that you read "Extending Internal Classes" before reading this section. Doing so will familiarize you with the patterns used by the default routes, which is the best practice. While it is not required that the class you use to process your request extends the `WP_REST_Controller` class or a class that extends it, doing so allows you to inherit work done in those classes. In addition, you can rest assured that you're following best practices based on the controller methods you're using.
246
+
It is recommended that you read ["Extending Internal Classes"](/extending/internal-classes/) before reading this section. Doing so will familiarize you with the patterns used by the default routes, which is the best practice. While it is not required that the class you use to process your request extends the `WP_REST_Controller` class or a class that extends it, doing so allows you to inherit work done in those classes. In addition, you can rest assured that you're following best practices based on the controller methods you're using.
244
247
245
248
At their core, controllers are nothing more than a set of commonly named methods to match up with REST conventions, along with some handy helpers. Controllers register their routes in a `register_routes` method, respond to requests with `get_items`, `get_item`, `create_item`, `update_item` and `delete_item`, and have similarly named permission check methods. Following this pattern will ensure you don't miss any steps or functionality in your endpoints.
Copy file name to clipboardExpand all lines: extending/javascript-client/index.md
+26-17
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ Models:
31
31
* Page
32
32
* PageMeta
33
33
* PageRevision
34
-
* Posts
34
+
* Post
35
35
* PostMeta
36
36
* PostRevision
37
37
* Schema
@@ -63,7 +63,7 @@ You can use these endpoints as-is to read, update, create and delete items using
63
63
Each model and collection includes a reference to its default values, for example:
64
64
65
65
```
66
-
wp.api.models.Posts.defaults
66
+
wp.api.models.Post.prototype.args
67
67
* author: null
68
68
* comment_status: null
69
69
* content: null
@@ -84,7 +84,7 @@ wp.api.models.Posts.defaults
84
84
85
85
### Available methods
86
86
87
-
Each model and collection contains a list of methods the corrosponding endpoint supports. For example, models created from `wp.api.models.Posts` have a method array of:
87
+
Each model and collection contains a list of methods the corresponding endpoint supports. For example, models created from `wp.api.models.Post` have a methods array of:
88
88
89
89
```
90
90
["GET", "POST", "PUT", "PATCH", "DELETE"]
@@ -95,7 +95,7 @@ Each model and collection contains a list of methods the corrosponding endpoint
95
95
Each model and collection contains a list of options the corrosponding endpoint accepts (note that options are passed as the second parameter when creating models or collections), for example:
To create a post and edit its categories, make sure you are logged in, then:
110
+
### Waiting for the client to load
111
+
Client startup is asynchronous. If the api schema is localized, the client can start immediately; if not the client makes an ajax request to load the schema. The client exposes a load promise for provide a reliable wait to wait for client to be ready:
112
112
113
+
```js
114
+
wp.api.loadPromise.done( function() {
115
+
//... use the client here
116
+
} )
113
117
```
114
-
// Create a new post
115
-
var post = new wp.api.models.Posts( { title: 'This is a test post' } );
116
-
post.save();
117
118
119
+
### Model examples:
120
+
121
+
To create a post and edit its categories, make sure you are logged in, then:
122
+
123
+
```js
118
124
// Create a new post
119
-
var post = new wp.api.models.Posts({ title:'new test' } );
125
+
var post =newwp.api.models.Post( { title:'This is a test post' } );
120
126
post.save();
121
127
122
-
// Get a collection of the post's categories
123
-
var postCategories = post.getCategories();
128
+
// Load an existing post
129
+
var post =newwp.api.models.Post( { id:1 } );
130
+
post.fetch();
124
131
125
132
// Get a collection of the post's categories (returns a promise)
126
133
// Uses _embedded data if available, in which case promise resolves immediately.
0 commit comments