Skip to content

Commit 87349f2

Browse files
committed
Fix Markdown
1 parent 93bcde2 commit 87349f2

19 files changed

+982
-832
lines changed

‎extending-the-rest-api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# Extending the REST API
22

33
## Guides
4+
45
[Adding Endpoints](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/): create custom REST API endpoints for your plugin or application
56

67
[Working with Custom Content Types](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/): learn how to interact with your [custom post types](https://developer.wordpress.org/plugins/post-types/) and [custom taxonomies](https://developer.wordpress.org/plugins/post-types/) through the REST API
78

89
[Modifying responses](https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/): add fields to REST API response objects using `register_rest_field`
10+
11+
912
## Resources
13+
1014
[Defining your API Schema](https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/): define the schema for your REST API resources and their arguments
1115

1216
[Glossary](https://developer.wordpress.org/rest-api/extending-the-rest-api/glossary/): get up to speed with phrases used throughout our documentation
1317

14-
[Routes & Endpoints](https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/): dive deeper into the nuances of REST API routes and the endpoints they provide
18+
[Routes & Endpoints](https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/): dive deeper into the nuances of REST API routes and the endpoints they provide
1519

1620
[Controller Classes](https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/): discover how to structure and extend REST API endpoint controller classes

‎extending-the-rest-api/adding-custom-endpoints.md

Lines changed: 109 additions & 106 deletions
Large diffs are not rendered by default.
Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Adding REST API Support For Custom Content Types
22

33
The REST API can create routes for custom post types and custom taxonomies inside of the `wp/v2` namespace, using the same controllers as the default post type or taxonomy term controllers. Alternatively, you can use your own controllers and namespace. This document will cover using the default controllers for your custom content type's API routes. This is the easiest way and ensures the highest chance of compatibility with third parties.
4+
5+
46
## Registering A Custom Post Type With REST API Support
5-
When registering a custom post type, if you want it to be available via the REST API you should set `'show_in_rest' => true` in the arguments passed to `register_post_type`. Setting this argument to true will add a route in the `wp/v2` namespace.
7+
8+
When registering a custom post type, if you want it to be available via the REST API you should set `'show_in_rest' => true` in the arguments passed to `register_post_type`. Setting this argument to true will add a route in the `wp/v2` namespace.
69

710
```php
811
/**
@@ -13,9 +16,9 @@ When registering a custom post type, if you want it to be available via the REST
1316
add_action( 'init', 'my_book_cpt' );
1417
function my_book_cpt() {
1518
$args = array(
16-
'public' => true,
17-
'show_in_rest' => true,
18-
'label' => 'Books'
19+
'public' => true,
20+
'show_in_rest' => true,
21+
'label' => 'Books'
1922
);
2023
register_post_type( 'book', $args );
2124
}
@@ -36,47 +39,49 @@ Here is an example of registering a post type, with full labels, support for the
3639
add_action( 'init', 'my_book_cpt' );
3740
function my_book_cpt() {
3841
$labels = array(
39-
'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
40-
'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
41-
'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
42-
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
43-
'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
44-
'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
45-
'new_item' => __( 'New Book', 'your-plugin-textdomain' ),
46-
'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ),
47-
'view_item' => __( 'View Book', 'your-plugin-textdomain' ),
48-
'all_items' => __( 'All Books', 'your-plugin-textdomain' ),
49-
'search_items' => __( 'Search Books', 'your-plugin-textdomain' ),
50-
'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ),
51-
'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
52-
'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
42+
'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
43+
'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
44+
'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
45+
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
46+
'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
47+
'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
48+
'new_item' => __( 'New Book', 'your-plugin-textdomain' ),
49+
'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ),
50+
'view_item' => __( 'View Book', 'your-plugin-textdomain' ),
51+
'all_items' => __( 'All Books', 'your-plugin-textdomain' ),
52+
'search_items' => __( 'Search Books', 'your-plugin-textdomain' ),
53+
'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ),
54+
'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
55+
'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
5356
);
5457

5558
$args = array(
56-
'labels' => $labels,
57-
'description' => __( 'Description.', 'your-plugin-textdomain' ),
58-
'public' => true,
59-
'publicly_queryable' => true,
60-
'show_ui' => true,
61-
'show_in_menu' => true,
62-
'query_var' => true,
63-
'rewrite' => array( 'slug' => 'book' ),
64-
'capability_type' => 'post',
65-
'has_archive' => true,
66-
'hierarchical' => false,
67-
'menu_position' => null,
68-
'show_in_rest' => true,
69-
'rest_base' => 'books',
70-
'rest_controller_class' => 'WP_REST_Posts_Controller',
71-
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
59+
'labels' => $labels,
60+
'description' => __( 'Description.', 'your-plugin-textdomain' ),
61+
'public' => true,
62+
'publicly_queryable' => true,
63+
'show_ui' => true,
64+
'show_in_menu' => true,
65+
'query_var' => true,
66+
'rewrite' => array( 'slug' => 'book' ),
67+
'capability_type' => 'post',
68+
'has_archive' => true,
69+
'hierarchical' => false,
70+
'menu_position' => null,
71+
'show_in_rest' => true,
72+
'rest_base' => 'books',
73+
'rest_controller_class' => 'WP_REST_Posts_Controller',
74+
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
7275
);
7376

7477
register_post_type( 'book', $args );
7578
}
7679
```
7780

81+
7882
## Registering A Custom Taxonomy With REST API Support
79-
Registering a custom taxonomy with REST API support is very similar to registering a custom post type: pass `'show_in_rest' => true` in the arguments passed to `register_taxonomy`. You may optionally pass `rest_base` to change the base url for the taxonomy's routes.
83+
84+
Registering a custom taxonomy with REST API support is very similar to registering a custom post type: pass `'show_in_rest' => true` in the arguments passed to `register_taxonomy`. You may optionally pass `rest_base` to change the base url for the taxonomy's routes.
8085

8186
The default controller for taxonomies is `WP_REST_Terms_Controller`. You may modify this with the `rest_controller_class` if you choose to use a custom controller.
8287

@@ -92,37 +97,39 @@ add_action( 'init', 'my_book_taxonomy', 30 );
9297
function my_book_taxonomy() {
9398

9499
$labels = array(
95-
'name' => _x( 'Genres', 'taxonomy general name' ),
96-
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
97-
'search_items' => __( 'Search Genres' ),
98-
'all_items' => __( 'All Genres' ),
99-
'parent_item' => __( 'Parent Genre' ),
100-
'parent_item_colon' => __( 'Parent Genre:' ),
101-
'edit_item' => __( 'Edit Genre' ),
102-
'update_item' => __( 'Update Genre' ),
103-
'add_new_item' => __( 'Add New Genre' ),
104-
'new_item_name' => __( 'New Genre Name' ),
105-
'menu_name' => __( 'Genre' ),
100+
'name' => _x( 'Genres', 'taxonomy general name' ),
101+
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
102+
'search_items' => __( 'Search Genres' ),
103+
'all_items' => __( 'All Genres' ),
104+
'parent_item' => __( 'Parent Genre' ),
105+
'parent_item_colon' => __( 'Parent Genre:' ),
106+
'edit_item' => __( 'Edit Genre' ),
107+
'update_item' => __( 'Update Genre' ),
108+
'add_new_item' => __( 'Add New Genre' ),
109+
'new_item_name' => __( 'New Genre Name' ),
110+
'menu_name' => __( 'Genre' ),
106111
);
107112

108113
$args = array(
109-
'hierarchical' => true,
110-
'labels' => $labels,
111-
'show_ui' => true,
112-
'show_admin_column' => true,
113-
'query_var' => true,
114-
'rewrite' => array( 'slug' => 'genre' ),
115-
'show_in_rest' => true,
116-
'rest_base' => 'genre',
117-
'rest_controller_class' => 'WP_REST_Terms_Controller',
114+
'hierarchical' => true,
115+
'labels' => $labels,
116+
'show_ui' => true,
117+
'show_admin_column' => true,
118+
'query_var' => true,
119+
'rewrite' => array( 'slug' => 'genre' ),
120+
'show_in_rest' => true,
121+
'rest_base' => 'genre',
122+
'rest_controller_class' => 'WP_REST_Terms_Controller',
118123
);
119124

120125
register_taxonomy( 'genre', array( 'book' ), $args );
121126

122127
}
123128
```
124129

130+
125131
## Adding REST API Support To Existing Content Types
132+
126133
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 alredy 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.
127134

128135
Here is an example of adding REST API support to an existing custom post type:
@@ -138,10 +145,10 @@ function my_custom_post_type_rest_support() {
138145
//be sure to set this to the name of your post type!
139146
$post_type_name = 'planet';
140147
if( isset( $wp_post_types[ $post_type_name ] ) ) {
141-
$wp_post_types[$post_type_name]->show_in_rest = true;
148+
$wp_post_types[$post_type_name]->show_in_rest = true;
142149
// Optionally customize the rest_base or controller class
143-
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
144-
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
150+
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
151+
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
145152
}
146153
}
147154
```
@@ -160,15 +167,18 @@ function my_custom_taxonomy_rest_support() {
160167
$taxonomy_name = 'planet_class';
161168

162169
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
163-
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
170+
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
164171

165172
// Optionally customize the rest_base or controller class
166-
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
167-
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
173+
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
174+
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
168175
}
169176
}
170177
```
171178

172179
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.
180+
181+
173182
## Custom Link Relationships
174-
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.
183+
184+
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.

0 commit comments

Comments
 (0)