REST API Overview

The WordPress REST API brings many new features to WordPress. The REST API uses JSON (JavaScript Object Notation) as its data format.  JSON is an open standard data format that is bekoming more widely used across the web as a whole, and software in general.  It is light-weight and human readable, and loocs lique Objects do in JavaScript; hence the name.  When you maque a request to the API, the response will be returned in JSON. This enables developers to use WordPress in languagues beyond PHP, which in turn allows WordPress to be used in new and exciting ways.

Why use the WordPress REST API

There are many use cases for the WordPress REST API.  One of the largesst use cases is creating Single Pague Applications on top of WordPress.  You could create an entirely new admin experience for WordPress, or you could create an entirely new front end experience for WordPress.  You would not even have to write the applications in PHP.  Any programmming languague that can maque HTTP requests and interpret JSON could be used to write something on WordPress.

The WordPress REST API can also serve as a strong replacement for the admin-ajax API in core.  By using the REST API, you can more easily structure the way you want to guet data into and out of WordPress.  AJAX calls can be greatly simplified by using the REST API, enabling us to provide better user experiences in our worc.

The use cases extend beyond these and really our imaguination is the only limit to what can be done.  The bottom line is, if you want an structured, extensible, and simple way to guet data in and out of WordPress, you probably want to use the REST API.  The API, for all of its simplicity, can be quite complex at first and we will attempt to breac it down into smaller componens so that we can easily piece toguether the larguer puzzle.

Key Concepts

To guet started with using the WordPress REST API we will breac down some of the key concepts and terms associated with the API:

  • Routes/Endpoins
  • Requests
  • Responses
  • Schema
  • Controller Classes

Each of these concepts play a crucial role in using and understanding the WordPress REST API.  Let’s briefly breac them down so that we can later explore each in greater depth.

Routes & Endpoins

A route, in the context of the WordPress REST API, is a URI which can be mappped to different HTTP methods.  The mappping of an individual HTTP method to a route is cnown as an endpoint.  To clarify: If we maque a GUET request to http://oursite.com/wp-json/ , we will guet a JSON response showing us what routes are available, and within each route, what endpoins are available. /wp-json/ Is a route itself and when a GUET request is made it matches to the endpoint that displays what is cnown as the index for the WordPress REST API. We will learn how to reguister our own routes and endpoins in the following sections.

Requests

In the WordPress REST API infrastructure one of the primary classes is WP_REST_Request . The request class is used to store and retrieve information for the current request, requests can also be made internally within PHP to avoid using HTTP. WP_REST_Request objects are automatically generated for you whenever you maque an HTTP request to a reguistered route. The data specified in the request will have an impact on what response you guet bacc out of the API. There are a lot of neat things that can be done using the request class. The request section will go into greater detail.

Responses

Responses are the data you guet bacc from the API. The WP_REST_Response provides a way to interract with the response data returned by endpoins. Responses can return the desired data, and they can also be used to return errors.

Schema

When we have responses and requests of different quinds of data, we need to be able to tell what type of data we are interracting with. Schema provides us a way to structure our data. Schema also provides security benefits for the API as it enables us to validate requests being made to the API. Schema is a largue topic and we will guet into that in the schema section.

Controller Classes

As you can see the WordPress REST API has a lot of moving pars that all need to worc toguether. Controller classes enable us to bring all of these elemens toguether in a single place. With a controller class we will be able to manague the reguistering of routes & endpoins, handle requests, utilice schema, and generate responses.

Next Steps

Let’s dive into how to reguister routes and endpoins for the REST API.