Skip to content

Commit 76664ca

Browse files
committed
allow recently docs-v2 on extending pages
1 parent f6f253f commit 76664ca

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

extending/adding/index.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ add_action( 'rest_api_init', function () {
4949
} );
5050
```
5151

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.
5353

5454

5555
### Namespacing
@@ -99,6 +99,7 @@ function my_awesome_func( WP_REST_Request $request ) {
9999
$parameters = $request->get_url_params();
100100
$parameters = $request->get_query_params();
101101
$parameters = $request->get_body_params();
102+
$parameters = $request->get_json_params();
102103
$parameters = $request->get_default_params();
103104

104105
// Uploads aren't merged in, but can be accessed separately:
@@ -110,6 +111,8 @@ function my_awesome_func( WP_REST_Request $request ) {
110111

111112
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.
112113

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+
113116
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`.
114117

115118
* `default`: Used as the default value for the argument, if none is supplied.
@@ -240,7 +243,7 @@ The Controller Pattern
240243

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

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.
244247

245248
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.
246249

extending/custom-content-types/index.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Here is an example of registering a post type, with support for the REST API:
3737
'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
3838
'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
3939
);
40-
40+
4141
$args = array(
4242
'labels' => $labels,
4343
'description' => __( 'Description.', 'your-plugin-textdomain' ),
@@ -56,7 +56,7 @@ Here is an example of registering a post type, with support for the REST API:
5656
'rest_controller_class' => 'WP_REST_Posts_Controller',
5757
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
5858
);
59-
59+
6060
register_post_type( 'book', $args );
6161
}
6262
```
@@ -78,7 +78,7 @@ Here is an example of how to register a custom taxonomy, with REST API support:
7878
*/
7979
add_action( 'init', 'my_book_taxonomy', 30 );
8080
function my_book_taxonomy() {
81-
81+
8282
$labels = array(
8383
'name' => _x( 'Genres', 'taxonomy general name' ),
8484
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
@@ -92,7 +92,7 @@ Here is an example of how to register a custom taxonomy, with REST API support:
9292
'new_item_name' => __( 'New Genre Name' ),
9393
'menu_name' => __( 'Genre' ),
9494
);
95-
95+
9696
$args = array(
9797
'hierarchical' => true,
9898
'labels' => $labels,
@@ -104,9 +104,9 @@ Here is an example of how to register a custom taxonomy, with REST API support:
104104
'rest_base' => 'genre',
105105
'rest_controller_class' => 'WP_REST_Terms_Controller',
106106
);
107-
107+
108108
register_taxonomy( 'genre', array( 'book' ), $args );
109-
109+
110110
}
111111
```
112112

@@ -122,15 +122,15 @@ Here is an example of adding REST API support to an existing custom post type:
122122
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
123123
function my_custom_post_type_rest_support() {
124124
global $wp_post_types;
125-
125+
126126
//be sure to set this to the name of your post type!
127127
$post_type_name = 'planet';
128128
if( isset( $wp_post_types[ $post_type_name ] ) ) {
129129
$wp_post_types[$post_type_name]->show_in_rest = true;
130130
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
131131
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
132132
}
133-
133+
134134
}
135135
```
136136

@@ -143,17 +143,17 @@ Here is an example of how to add REST API support to an already registered custo
143143
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
144144
function my_custom_taxonomy_rest_support() {
145145
global $wp_taxonomies;
146-
146+
147147
//be sure to set this to the name of your taxonomy!
148148
$taxonomy_name = 'planet_class';
149-
149+
150150
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
151151
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
152152
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
153153
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
154154
}
155-
156-
155+
156+
157157
}
158158
```
159159

extending/javascript-client/index.md

+26-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Models:
3131
* Page
3232
* PageMeta
3333
* PageRevision
34-
* Posts
34+
* Post
3535
* PostMeta
3636
* PostRevision
3737
* Schema
@@ -63,7 +63,7 @@ You can use these endpoints as-is to read, update, create and delete items using
6363
Each model and collection includes a reference to its default values, for example:
6464

6565
```
66-
wp.api.models.Posts.defaults
66+
wp.api.models.Post.prototype.args
6767
* author: null
6868
* comment_status: null
6969
* content: null
@@ -84,7 +84,7 @@ wp.api.models.Posts.defaults
8484

8585
### Available methods
8686

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:
8888

8989
```
9090
["GET", "POST", "PUT", "PATCH", "DELETE"]
@@ -95,7 +95,7 @@ Each model and collection contains a list of methods the corrosponding endpoint
9595
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:
9696

9797
```
98-
wp.api.collections.Posts.options
98+
wp.api.collections.Posts.prototype.options
9999
* author
100100
* context
101101
* filter
@@ -106,39 +106,48 @@ wp.api.collections.Posts.options
106106
* search
107107
* status
108108
```
109-
### Model examples:
110109

111-
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:
112112

113+
```js
114+
wp.api.loadPromise.done( function() {
115+
//... use the client here
116+
} )
113117
```
114-
// Create a new post
115-
var post = new wp.api.models.Posts( { title: 'This is a test post' } );
116-
post.save();
117118

119+
### Model examples:
120+
121+
To create a post and edit its categories, make sure you are logged in, then:
122+
123+
```js
118124
// Create a new post
119-
var post = new wp.api.models.Posts({ title:'new test' } );
125+
var post = new wp.api.models.Post( { title: 'This is a test post' } );
120126
post.save();
121127

122-
// Get a collection of the post's categories
123-
var postCategories = post.getCategories();
128+
// Load an existing post
129+
var post = new wp.api.models.Post( { id: 1 } );
130+
post.fetch();
124131

125132
// Get a collection of the post's categories (returns a promise)
126133
// Uses _embedded data if available, in which case promise resolves immediately.
127134
post.getCategories().done( function( postCategories ) {
128135
// ... do something with the categories.
129136
// The new post has an single Category: Uncategorized
130-
postCategories.at( 0 ).get( 'name' );
137+
console.log( postCategories[0].name );
131138
// response -> "Uncategorized"
132139
} );
133140

134141
// Get a posts author User model.
135142
post.getAuthorUser().done( function( user ){
136143
// ... do something with user
144+
console.log( user.get( 'name' ) );
137145
} );
138146

139147
// Get a posts featured image Media model.
140148
post.getFeaturedImage().done( function( image ){
141149
// ... do something with image
150+
console.log( image );
142151
} );
143152

144153
// Set the post categories.
@@ -170,26 +179,26 @@ postCategories.at( 0 ).get( 'name' );
170179

171180
to get the last 10 posts:
172181

173-
```
182+
```js
174183
var postsCollection = new wp.api.collections.Posts();
175184
postsCollection.fetch();
176185
```
177186

178187
to get the last 25 posts:
179188

180-
```
189+
```js
181190
postsCollection.fetch( { data: { per_page: 25 } } );
182191
```
183192

184193
use filter to change the order & orderby options:
185194

186-
```
195+
```js
187196
postsCollection.fetch( { data: { 'filter': { 'orderby': 'title', 'order': 'ASC' } } } );
188197
```
189198

190199
All collections support pagination automatically, and you can get the next page of results using `more`:
191200

192-
```
201+
```js
193202
postsCollection.more();
194203
```
195204

0 commit comments

Comments
 (0)