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: index.md
+14-17Lines changed: 14 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
The WordPress REST API provides an interface for applications to interact with your WordPress site by sending and receiving data as [JSON](https://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) objects. It is the foundation of the [WordPress Block Editor](https://developer.wordpress.org/block-editor/), and can likewise enable your theme, plugin or custom application to present new, powerful interfaces for managing and publishing your site content.
4
4
5
-
An API is an Application Programming Interface. REST, standing for "REpresentational State Transfer," is a set of concepts for modeling and accessing your application's data as interrelated objects and collections. The WordPress REST API provides REST endpoints (URLs) representing the posts, pages, taxonomies, and other built-in WordPress data types. Your application can send and receive JSON data to these endpoints to query, modify & create content on your site. JSON is an open standard data format that is lightweight and human-readable, and looks like Objects do in JavaScript. When you request content from or send content to the API, the response will also be returned in JSON. Because JSON is widely supported in many programming languages, developers can build WordPress applications in client-side JavaScript (like the block editor), as mobile apps, or as desktop or command line tools.
5
+
An API is an Application Programming Interface. REST, standing for "REpresentational State Transfer," is a set of concepts for modeling and accessing your application's data as interrelated objects and collections. The WordPress REST API provides REST endpoints (URLs) representing the posts, pages, taxonomies, and other built-in WordPress data types. Your application can send and receive JSON data to these endpoints to query, modify and create content on your site. JSON is an open standard data format that is lightweight and human-readable, and looks like Objects do in JavaScript. When you request content from or send content to the API, the response will also be returned in JSON. Because JSON is widely supported in many programming languages, developers can build WordPress applications in client-side JavaScript (like the block editor), as mobile apps, or as desktop or command line tools.
6
6
7
7
[info]The REST API is just one of many APIs provided by WordPress. You can find the [documentation on these additional APIs here](https://codex.wordpress.org/WordPress_APIs).[/info]
8
8
@@ -16,48 +16,45 @@ Using the WordPress REST API you can create a plugin to provide an entirely new
16
16
17
17
If you want a structured, extensible, and simple way to get data in and out of WordPress, you probably want to use the REST API.
18
18
19
-
For all of its simplicity the REST API can feelquite complex at first, so in this handbook we will attempt to break it down into smaller components to explain each part of the fullpuzzle.
19
+
For all of its simplicity the REST API can feelquite complex at first, so in this handbook we will attempt to break it down into smaller components to explain each part of the fullpuzzle.
20
20
21
21
22
22
## Key Concepts
23
23
24
-
To get started with using the WordPress REST API we will break down some of the key concepts and terms associated with the API:
24
+
To get started we will break down some of the key concepts and terms associated with the REST API:**Routes & Endpoints,****Requests**, **Responses**, **Schema**, and **Controller Classes**. Each of these concepts play a crucial role in understanding, using, and extending the WordPress REST API, and each is explored in greater depth within this handbook.
25
25
26
-
* Routes/Endpoints
27
-
* Requests
28
-
* Responses
29
-
* Schema
30
-
* Controller Classes
26
+
### Routes & Endpoints
31
27
32
-
Each of these concepts play a crucial role in using and understanding the WordPress REST API. Let's briefly break them down so that we can later explore each in greater depth.
28
+
In the context of the WordPress REST API a **route** 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**.
33
29
34
-
### Routes & Endpoints
30
+
As an example, if we make a `GET` request to the URI `http://oursite.com/wp-json/` we are returned a JSON response showing what routes are available, and what endpoints are available within each route. `/wp-json/` is a route, and when that route receives a `GET` request then that request is handled by the endpoint which displays what is known as the index for the WordPress REST API. The route `wp-json/wp/v2/posts` by contrast has a `GET` endpoint which returns a list of posts, but also a `POST` endpoint which accepts authenticated requests to create new posts.
35
31
36
-
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.
32
+
We will learn how to register our own routes and endpoints in the following sections.
37
33
38
-
[info]If you're using [non-pretty permalinks](https://wordpress.org/support/article/using-permalinks/), you should pass the REST API route as a query string parameter. The route `http://oursite.com/wp-json/` in the example above would hence be `http://oursite.com/?rest_route=/`.[/info]
34
+
[info]If you are using [non-pretty permalinks](https://wordpress.org/support/article/using-permalinks/), you should pass the REST API route as a query string parameter. The route `http://oursite.com/wp-json/` in the example above would hence be `http://oursite.com/?rest_route=/`.[/info]
39
35
40
36
If you get a `404` error when trying to access `http://oursite.com/wp-json/`, consider enabling pretty permalinks or try using the `rest_route` parameter instead.
41
37
42
38
### Requests
43
39
44
-
One of the primary classes in the WordPress REST API infrastructure is `WP_REST_Request`. This classis used to store and retrieve information for the current request; requests can be submitted remotely via HTTP but may also be made internally from PHP with WordPress. `WP_REST_Request`objects are automatically generated for you whenever you make an HTTP request to a registered route. The data specified in the request will determine what response you get back out of the API. There are a lot of neat things you can do using the request class. The request section will go into greater detail.
40
+
A REST API request is represented within WordPress by an instance of the `WP_REST_Request`class, which is used to store and retrieve information for the current request. A `WP_REST_Request`object is automatically generated when you make an HTTP request to a registered API route. The data specified in this object (derived from the route URI or the JSON payload sent as a part of the request) determines what response you will get back out of the API.
45
41
42
+
Requests are usually submitted remotely via HTTP but may also be made internally from PHP within WordPress plugin or theme code. There are a lot of neat things you can do using this class, detailed further elsewhere in the handbook.
46
43
47
44
### Responses
48
45
49
-
Responses are the data you get back from the API. The `WP_REST_Response` class provides a way to interact with the response data returned by endpoints. Responses can return the desired data, and they can also be used to return errors.
46
+
Responses are the data you get back from the API. The `WP_REST_Response` class provides a way to interact with the response data returned by endpoints. Responses return the requested data, or can also be used to return errors if something goes wrong while fulfilling the request.
50
47
51
48
### Schema
52
49
53
-
Each endpoint requires and provides slightly different data structures, and those structures are defined in the API Schema. The schemastructures API data and provides a comprehensive list of all of the properties the API can returnand input parameters it can accept. Schema also provides security benefits for the API, as it enables usto validate the requests being made to the API. The Schema section further exploresthislarge topic.
50
+
Each endpoint requires a particular structure of input data, and returns data using a defined and predictable structure. Those data structures are defined in the API Schema. The schemastructures API data and provides a comprehensive list of all of the properties the API can returnand which input parameters it can accept. Well-defined schema also provides one layer of security within the API, as it enables usto validate and sanitize the requests being made to the API. The [Schema section](https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/)exploresthislarge topic further.
54
51
55
52
56
53
### Controller Classes
57
54
58
-
As you can see, the WordPress REST API has a lot of moving parts that all need to work together. Controller classes bring all of these elements together in a single place. With a controller class you canmanage the registrationof routes & endpoints, handle requests, utilize schema, and generate API responses.
55
+
Controller classes unify and coordinate all these various moving parts within a REST API response cycle. With a controller class you canmanage the registrationof routes & endpoints, handle requests, utilize schema, and generate API responses. A single class usually contains all of the logic for a given route, and a given route usually represents a specific type of data object within your WordPress site (like a custom post type or taxonomy).
59
56
60
57
61
58
## Next Steps
62
59
63
-
Let's look at the reference for the WordPress REST API.
60
+
For a comprehensive overview of the resources and routes available within the default WordPress REST API, review the [API reference](https://developer.wordpress.org/rest-api/reference/). To learn more about how to interact with API resources, you can read through the [Using the REST API section](https://developer.wordpress.org/rest-api/using-the-rest-api/). Once you're comfortable with the default workings of the default routes and methods you may also dive in to how to [extend the REST API](https://developer.wordpress.org/rest-api/extending-the-rest-api/) to expose new data or enhance and manipulate existing response objects.
0 commit comments