Skip to content

Commit 04855af

Browse files
authored
Merge branch 'master' into patch-1
2 parents d69d4ca + 84f9501 commit 04855af

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

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

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -128,55 +128,52 @@ function my_book_taxonomy() {
128128
```
129129

130130
## Adding REST API Support To Existing Content Types
131-
When a custom post type or custom taxonomy has been added by code that you do not control, for example a theme or plugin you are using, you may need to add REST API support after it has already been registered. The arguments are the same as in the previous examples, but need to be added to the global `$wp_post_types` and `$wp_taxonomies` arrays.
132131

133-
Here is an example of adding REST API support to an existing custom post type:
132+
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.
134133

135134
```php
136135
/**
137136
* Add REST API support to an already registered post type.
138137
*/
139-
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
140-
function my_custom_post_type_rest_support() {
141-
global $wp_post_types;
142-
143-
//be sure to set this to the name of your post type!
144-
$post_type_name = 'planet';
145-
if( isset( $wp_post_types[ $post_type_name ] ) ) {
146-
$wp_post_types[$post_type_name]->show_in_rest = true;
147-
// Optionally customize the rest_base or controller class
148-
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
149-
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
150-
}
138+
add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
139+
140+
function my_post_type_args( $args, $post_type ) {
141+
142+
if ( 'book' === $post_type ) {
143+
$args['show_in_rest'] = true;
144+
145+
// Optionally customize the rest_base or rest_controller_class
146+
$args['rest_base'] = 'books';
147+
$args['rest_controller_class'] = 'WP_REST_Posts_Controller';
148+
}
149+
150+
return $args;
151151
}
152152
```
153153

154-
Here is an example of how to add REST API support to an already registered custom taxonomy.
154+
155+
For custom taxnomies it is almost the same. You can use the `register_taxonomy_args` filter that exists since WordPress version 4.4.0.
155156

156157
```php
157158
/**
158159
* Add REST API support to an already registered taxonomy.
159160
*/
160-
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
161-
function my_custom_taxonomy_rest_support() {
162-
global $wp_taxonomies;
161+
add_filter( 'register_taxonomy_args', 'my_taxonomy_args', 10, 2 );
162+
163+
function my_taxonomy_args( $args, $taxonomy_name ) {
163164

164-
//be sure to set this to the name of your taxonomy!
165-
$taxonomy_name = 'planet_class';
165+
if ( 'genre' === $taxonomy_name ) {
166+
$args['show_in_rest'] = true;
166167

167-
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
168-
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
168+
// Optionally customize the rest_base or rest_controller_class
169+
$args['rest_base'] = 'genres';
170+
$args['rest_controller_class'] = 'WP_REST_Terms_Controller';
171+
}
169172

170-
// Optionally customize the rest_base or controller class
171-
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
172-
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
173-
}
173+
return $args;
174174
}
175175
```
176176

177-
If you are having trouble implementing either of these examples, be sure that you are adding these hooks with a sufficiently high priority. If the callback functions run before the post type or taxonomy is registered, then the `isset` check will prevent an error, but the support will not be added.
178-
179-
180177
## Custom Link Relationships
181178

182179
Taxonomies & custom post types have a built-in association within WordPress, but what if you want to establish a link between two custom post types? This is not supported formally within WordPress itself, but we can create our own connections between arbitrary content types using the `_link` relation.

index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Each of these concepts play a crucial role in using and understanding the WordPr
3333

3434
A route, in the context of the WordPress REST API, is a URI which can be mapped to different HTTP methods. The mapping of an individual HTTP method to a route is known as an "endpoint". To clarify: If we make a `GET` request to `http://oursite.com/wp-json/`, we will get a JSON response showing us what routes are available, and within each route, what endpoints are available. `/wp-json/` is a route itself and when a `GET` request is made it matches to the endpoint that displays what is known as the index for the WordPress REST API. We will learn how to register our own routes and endpoints in the following sections.
3535

36-
If you get a `404` error when trying to access `http://oursite.com/wp-json/` URL you may try fixing your pretty permalinks. If you are using default permalinks (query params) you can access the API at `?rest_route=/`, for example `http://oursite.com/?rest_route=/`.
37-
36+
If you get a `404` error when trying to access `http://oursite.com/wp-json/` URL you may try fixing your pretty permalinks.
37+
[info] If you're using [non-pretty permalinks](https://codex.wordpress.org/Using_Permalinks), you should pass the REST API route as a query string parameter (with `?rest_route=/`, for example `http://oursite.com/?rest_route=/`). The route `http://oursite.com/wp-json/` in the example above would hence be `http://oursite.com/?rest_route=/`.[/info]
3838

3939
### Requests
4040

using-the-rest-api/authentication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $.ajax( {
5353
} );
5454
```
5555

56-
Note that you do not need to verify that the nonce in valid inside your custom end point. This is automatically done for you in
56+
Note that you do not need to verify that the nonce is valid inside your custom end point. This is automatically done for you in
5757
`rest_cookie_check_errors()`
5858

5959
## Authentication Plugins

using-the-rest-api/frequently-asked-questions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This page provides solutions to some common questions and problems that may aris
55

66
## Can I disable the REST API?
77

8-
You should not disable the REST API, because doing so would break future WordPress Admin functionality that will depend on the API being active. However, you may use a filter to require that API consumers be authenticated, which effectively prevents anonymous external access. See below for more information.
8+
You should not disable the REST API, because doing so will break WordPress Admin functionality that depends on the API being active. However, you may use a filter to require that API consumers be authenticated, which effectively prevents anonymous external access. See below for more information.
99

1010

1111
## Require Authentication for All Reque​sts

0 commit comments

Comments
 (0)