The WordPress REST API has been around for a while now, but understanding what it is and why it matters may not be immediately clear to newcomers and non-developers. Even so, this tool is a handy one to know for users of all stripes, as it enables you to utilize the familiar WordPress interface solely as a back end, and develop the front end using other tools and languages.
This post will provide an introduction to what the WordPress REST API is and what it’s capable of. We’ll also share a few steps to help you get started using WordPress as a back end for an app or website.
Let’s get to it!
There are many articles that explain what the WordPress REST API is – we’ve even covered this previously – so we’ll keep this brief to avoid repeating ourselves. Simply put, an Application Programming Interface (API) is what enables connections between various devices, applications, and data.
A helpful analogy is to think of an API as a waiter delivering requests and responses between diners and the kitchen. In this example, you’re the diner, the application or database you’re trying to communicate with is the kitchen, and your ‘meal’ is the data you’re requesting.
Representational State Transfer (REST) is an ‘architectural style’ or type of API. It’s designed to be lightweight and requires minimal bandwidth when used. Using a REST API, you can make four basic types of requests:
The WordPress REST API uses JavaScript Object Notation (JSON) to return data. So, when you put all of this together, you get a lightweight method of communicating GET, POST, PUT, and DELETE requests between WordPress websites and other platforms through the use of JSON.
It may seem like the WordPress REST API isn’t all that useful to you, particularly if you’re not a developer. However, there are many things the API can do that even intermediate WordPress users might want to try, such as:
A quick Google search will turn up numerous other ways in which the WordPress REST API is being used. However, for the rest of this post, we’ll focus on using WordPress as a back end for an app or website.
There are many ways to use WordPress as a back end. As such, this post is more of a general introduction to doing so using the WordPress REST API, rather than a complete guide to building an app or decoupled website.
While the steps below are simplified to help get you started, this means you’ll likely need to research additional guidance specific to your project. However, knowing these basics will help make the process easier.
The first step to using WordPress as a back end is remarkably similar to starting a WordPress website. You’ll need a WordPress installation loaded with all the content you want on your website or app. This could be an active installation or a new one you set up for your project.
You’ll also have to work on creating posts and pages, adding media files, and whatever else your project requires content-wise. Basically, you need to get everything you want to have on your site or app into WordPress, since this will be where you manage content.
Once you’ve got everything set up, you’ll likely want to run a test to see how the WordPress REST API works. Fortunately, you can do this right in your browser. Simply type in your domain followed by wp-json/wp/v2/posts:
You should see something like the image above, although the amount of JSON that’s displayed will differ depending on how populated the site is. You’ve now successfully used the WordPress REST API to return the data for all your posts. The REST API Handbook contains information on how to do this for other information as well.
Once you feel like you have a handle on how all of those pieces are working together, you can move on from the back end of your site to begin work on the front end.
Now you have the ‘behind-the-scenes’ aspects figured out, you’ll need to work on the part of your app or website that users will actually get to see. A popular route for accomplishing this for WordPress users is through React:
For the uninitated, React is (as the tagline in the image above reads) a JavaScript library for building user interfaces. It enables you to create elements called ‘components’, which become the pieces that make up the front end of your app or website. This might include posts, buttons, images, or any number of other features and functions.
There are also many other platforms you might want to consider for building your project, so shop around before you commit. The complete ins and outs of using React (or similar) are beyond the scope of this article. However, once you’ve settled on a method for creating your front end, take care of any configuration so that you’re ready to start building components.
By now, you should have your WordPress back end and your React (or other) front end. Now, you just need to get them to talk to one another so that the data from WordPress renders in your app or website. This is where the WordPress REST API comes in.
Since you’ve already tested the API out, and have seen that it can return data for your posts and other content, you know it’s working and ready to go. Next you’ll need to create components that use the REST API to retrieve posts from WordPress and display them on your front end.
If you have minimal coding experience, this is where things will get pretty tricky. If you’re hoping to launch your app or website quickly, you’re probably better off hiring a developer to set it up for you. However, if you’re willing to put in some time, you can start slowly working on components and learning as you go.
We’ve written about how to create a React component to retrieve all your WordPress posts and display them in an app in the past. You could use this post as an exercise to get started, or check out some online training courses.
Once you’ve used the WordPress REST API to get all of your content onto your front end, you can begin to create more (and increasingly complex) components, then work on customizing the front end of your site further – all while using WordPress to continue creating and managing content.
There’s definitely a learning curve when it comes to the WordPress REST API, especially if you have minimal coding knowledge. However, once you get the hang of it, this feature can help you expand on your basic foundation to use WordPress as a back end for your own app or custom website.
This post has provided some basic, simplified steps for using WordPress as a back end. Let’s recap them quickly:
Do you have any questions about the REST API or using WordPress as a back end? Ask them in the comments section below!