How To Use The WordPress REST API Plugin
An Application Programming Interface (API) (sometimes called the WP JSON REST API) is a type of software that enables two applications to work with each other by exchanging information. There are several types of APIs you can use, including Representational State Transfer (REST) options. A REST API is basically software that enables two applications to exchange data using a specific set of constraints.
In particular, the WordPress REST API enables you to connect your WordPress website with external applications. This means you can develop a mobile app using practically any programming language, and use the WP REST API to fetch data from WordPress. In a way, the REST API offers a way to free yourself from WordPress’ inherent structure, while harnessing it to help you create an application.
In this article, we’ll break down how the WordPress REST API works, and help you get started with it from scratch. Let’s get to work!
How the WordPress REST API Works
In the past, you needed a plugin in order to access the WordPress REST API. However, since Version 4.4, this WP API became a part of the core software. As such, to use the REST API, you simply need to know how to interact with it, which boils down to using four different types of HTTP methods as part of your requests:
- GET: With this method, you can fetch information from the server.
- POST: This enables you to send information to the server in question.
- PUT: With the put method, you can edit and update existing data.
- DELETE: This enables you to delete information.
As an example, consider what happens when you visit your WordPress login page. Your browser sends a GET request to the server, which processes it using its own API. Once the page loads, you enter your credentials and send them through a POST request. If you want to change your password, it involves the PUT method, whereas deleting your account altogether would use DELETE.
We’ll show you examples of how to use these methods with the WordPress REST API in a minute. For now, let’s go over some other concepts you’ll need to understand first.
Getting Familiar With the WordPress REST API
To really understand how the WordPress REST API works, there are a few tips and concepts you need to familiarize yourself with. When we get to real examples shortly, you’ll see how everything works in practice.
REST API Concepts and Terms
When using the WordPress REST API, you’ll see the same terms popping up over and over again, which include:
- ‘Routes’ and ‘endpoints’: A route is a URL you enter to make a request, whereas an endpoint is the combination of a URL with an HTTP method.
- Requests: When you submit an endpoint, you’re making a request to the server.
- Schemas: Every response you get follows similar structures, which are governed by built-in schemas.
- Controller classes: You can use these to build your own routes and endpoints, but this falls under more advanced WordPress REST API uses.
REST API Endpoints
In most cases, you’ll use routes and endpoints that already exist to submit requests via the WordPress REST API. Knowing what these endpoints are is the first step to mastering the WP API and using it to develop your own projects.
REST API Authentication
As you might expect, WordPress won’t let you access certain data unless it can corroborate who you are, and whether you’re requesting it via a browser or the REST API. For example, if you want to update or publish a post via commands, you’ll need to learn the basics of authentication.
WordPress REST API Use Examples
The REST API is being put to practical use on a number of major websites already. To give you some ideas for features you could implement, let’s look at a few REST API WordPress examples.
The USA Today site was rebuilt using the WordPress REST API, in order to facilitate integrations with other sites and third-party services. This enables it to easily push content to services such as Facebook and Apple News.
Naturally, the WordPress.com site makes heavy use of the WP API. In this case it’s on the back end, with admin pages that are built entirely using the API.
The New York Times
The New York Times leverages the WP REST API to run a live blog, where journalists can add important news developments in real-time. They’re even able to post to the blog directly from Slack thanks to the API, which enables a more seamless workflow.
How to Start Using the WordPress REST API (In 3 Steps)
We’ve gone over a lot of theory so far, so it’s time to move on to a WordPress endpoint tutorial. For this section, we’ll show you how to access the REST API, get back a list of specific data, and add new information using an endpoint. Let’s get to work!
Step 1: Access the REST API
You can ‘access’ the WordPress REST API from any application that can submit HTTP endpoints. For example, if you enter the following command within your favorite browser, you’ll get back a list of your WordPress posts in JSON format:
However, you’ll need to replace the placeholder URL with that of your own website. You’ll also need to use a version of WordPress greater than 4.4 for a REST API request to work (which you already should be doing).
If you want to really experiment with the REST API, though, a browser isn’t the best tool to do so. Instead, we recommend that you use the command line, which provides a more flexible approach.
Step 2: Fetch a Specific Post Using the REST API
The last command you ran should have returned a list of all your WordPress posts, including their post IDs. To fetch a specific post using its ID, you’d use an endpoint such as this:
For example, this would be ideal for showcasing a specific post translated within a mobile application. However, the WordPress REST API enables you to fetch all kinds of data from WordPress, so its practical applications are incredibly flexible.
However, let’s say you wanted to use the REST API to add metadata to a chosen post instead of merely fetching it. In other words, you’ll be using the POST method instead of GET.
Step 3: Add Metadata to a Specific Post
Assuming that you’ve already authenticated yourself, you can add new data to any of your posts using a similar request to that presented in the last section, using POST instead of GET:
For example, if you want to add metadata you could use to create a rich snippet for a recipe, the request may look like this:
Depending on how much metadata you want to add, you might want to specify it using JSON objects instead, which offers a much more structured approach. In any case, once you’re familiar with what the most common endpoints are and how to put them to use, a whole world of possibilities opens up.
Add Endpoints to WordPress
The WordPress REST API supports custom routes and endpoints. These are useful if you want to create a second WordPress site and add integrations between the two.
Routes in the REST API also support unlimited endpoints. You can specify HTTP methods, callback functions, and permissions, as well as default values and several other parameters for each endpoint.
Begin Building the WordPress Website That You Envision
The WordPress REST API has been around for a few years now. If you’ve never used it before, there’s a lot you need to learn to get to the point where you can use it to develop advanced applications.
At WP Engine, we offer you all the resources you need to help you learn as much as possible about the WordPress REST API and how to use it. While this guide offers a basic introduction, you’d also do well to read our Ultimate WordPress REST API e-book, as well as our guide for non-developers!