Using WordPress as a headless CMS

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

WordPress is still a widely used system, and it works for many clients since they are so used to it.
And to be quite honest, there is something about the admin panel that just makes sense. It's visual, easy to grasp, and well known.
However, what comes out of it, is somewhat of a nightmare sometimes. It's bloated, slow, and just not speed optimized. Sure we could opt to add yet another 20 plugins to make it quick, but is that a good approach?
Luckily WordPress comes as a headless CMS out of the box. Yes you heard me. It's already built-in (talk about bloated).
As plain as it gets, it means you are still using the WordPress backend to create your content but use REST API endpoints to retrieve the data in an external system.
You do lose your theme and plugin functionality, but let's take static site generators as our "front-end" they are blazing fast, secured, and might not even need all these plugins.
That will be my proof of concept to see how quickly we can make a WordPress headless CMS website.
But before diving into building something, let's take a good look at the API and what kind of data we can get out of it.
Before we start, the are two ways of calling the API.
For option one, which will be the most common and advised use case, we get an API on the http://yoursite.com/wp-json/ endpoint.
For the second option, we would have to query the API like this: http://yoursite.com/?rest_route=/.
However, I would recommend going with option one. You can enable this by going to the backend and visit the Settings/Permalinks section.
In there, click any of the structures, but not the plain one!
Time to try it out!
I'll be using Insomnia to query the API, but you can use any API client tool.

This basic call will give us all the available options we have. But we are more interested in retrieving pages and posts.
Let's have a look at the WordPress API reference to see what's available.
You can see we can call the posts through: /wp/v2/posts.
Let's give that a try.

Pretty cool, right! This endpoint returns all our posts in nice JSON format.
For the pages we see the /wp/v2/pages endpoint. Let's also try that one:

Pretty cool to see how extended the API is and what kind of data we can extract. And even if data is missing, we can add our endpoints for it.
In another article, I'll write about including this headless CMS as the data source for an Astro website.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter