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 WordPress tutorial, 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 WordPress plugin in order to access the WordPress JSON REST API. However, starting in WordPress Version 4.4, the WordPress REST 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.
Related Articles:
- Learn More About WordPress Progressive Web Apps
- Turn Your WordPress Site into a Mobile App
- Integrate WordPress & Laravel
- NGINX vs Apache: Which is Best for WordPress?
- Explore the Best WordPress CRM Plugins
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.
- Responses: If your endpoint is structured in the right way, you’ll get a response including the information you want in JavaScript Object Notation (JSON), or an error.
- 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 WordPress 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.
USA Today
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.
WordPress.com
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:
GET yourwebsiteurl.com/wp-json/wp/v2/posts
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:
GET yourwebsiteurl.com/wp-json/wp/v2/posts/535
For example, this would be ideal for showcasing a specific WordPress 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:
POST yourwebsiteurl.com/wp-json/wp/v2/posts/535/meta?value=newmetadata
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:
POST yourwebsiteurl.com/wp-json/wp/v2/posts/535/meta?cookingtime=25
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 You Envision
The WordPress REST API plays an important role in the larger WordPress ecosystem, as it offers extensive possibilities for web development. However, for those new to it, a significant learning curve awaits.
To harness its full potential, you’ll need a grasp of its core principles, a solid understanding of JSON data handling, and familiarity with aspects of authentication and security. As you delve deeper, you’ll realize the potential of the WordPress REST API to revolutionize WordPress development.
Building custom themes, plugins, mobile apps, and integrating with other platforms becomes seamless, unlocking the creation of sophisticated, integrated applications. There’s no question the WordPress REST API is a valuable asset in modern web development.
In addition to world-class hosting for WordPress, WP Engine offers you all the resources you need to help you learn as much as possible about WordPress development in general, 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 ebook, as well as our guide for non-developers!