The release of WordPress 4.7 generated a lot of excitement in the community, and with good reason. Besides the many improvements and features introduced, the real show stealer was the REST API core integration announcement. As we mentioned in previous posts, this effectively transforms WordPress from a blogging platform to a Content Management System (CMS). But how will this transformation take place? And what will be the benefits for the users?
In this article, we will first introduce you to some of fundamental concepts and then we will show you how you can start playing around with the API and your website.
All of our customers’ websites have already been upgraded to WordPress 4.7.1. So let’s begin!
What is an API?
API stands for Application Programming Interface. It’s a set of documented structures conforming to a standard functioning as LEGO blocks for software developers. These structures can be data structures, function calls, higher level components, pretty much anything. Software developers use APIs to build more complex software out of them. They are often used as a communication method between various software components, whether they are hosted locally (think software libraries) or remotely on the Internet, as is the case with REST APIs.
There are APIs for many different levels of software, ranging from the operating system to the web.
What is a REST API ?
REST stands for REpresentational State Transfer and enables the same interoperability and component communication, but on a higher level: the Web. With REST, you can have diverse web-based software written in Python, Java or PHP, communicating and exchanging data with each other, even if they are hosted in different systems. Since each system knows how to “speak” REST, it can speak with everyone that does!
REST uses the HTTP protocol as a communications transport and the standard HTTP requests to implement its basic operations. These operations are called verbs. The most commonly used ones are: PUT, GET, PATCH, DELETE. These verbs act on the things that REST calls Resources. They are implemented by the software developer and they effectively implement all four fundamental computer operations that work on storage: Create, Read, Update, Delete (CRUD).
How does WordPress implement it?
So we have the concept of verbs (Create, Read, Update, Delete) acting on Resources. WordPress defines as a REST Resource pretty much every discrete item of information that exists on the platform. From the users of a site to settings, posts, post taxonomies, notifications, media, stats, and everything in between. This includes the operations that can be defined for these Resources. For example, the operations defined for comments are the following:
- Get a list of recent comments.
- Get a list of recent comments on a post.
- Get a single comment.
- Edit a comment.
- Create a comment on a post.
- Create a comment as a reply to another comment.
- Delete a comment.
- Get the likes for a comment.
- Like a comment.
- Remove your like from a comment.
- Get your like status for a comment.
Take a look at the official API documentation page for WordPress at your ease, to get a feel of the API's breadth and depth and check what it can do. The possibilities are so many!
WordPress, thus, becomes a development platform and a CMS by treating every bit of data that exists on it as a REST Resource. It exposes all of them under a public, standardised API allowing external parties to perform CRUD operations on them. In this way, WordPress opens up and becomes interoperable with the rest of the software world, be it Java, Python or Ruby; and pretty much with every language or system that knows how to communicate using REST.
Try it out for yourself
There are several ways by which you can start tinkering with your website's REST API. The easiest one to use a REST testing plugin on your browser. There are several ones around but the most popular one is Postman (available for MacOSX, Windows and Chrome).
For example, let's say that you want to retrieve a JSON object containing all the posts of our blog. To do this, you first need to consult the official WordPress REST Reference, in order to find out the Base Route of that request call (it's /wp/v2/posts).
All we need to do now is the following:
- Select GET from the drop-down menu. There are many REST verbs available as you can see but covering all of them is not this article's scope.
- Append the Base Route (/wp/v2/posts) to your website's REST root URL (http://www.acme_shop_2.onpressidium.com/wp-json/) forming the URL you see on the screen.
- Click the Send button.
If all goes well, you'll be able to see your website's JSON response in the Body tab underneath. You can experiment with different Base Routes and Verbs by consulting the documentation. The WordPress REST API is something that we will concern ourselves with in future posts, in which we will make ample use of it, in more developer-oriented articles.
The future of WordPress is bright!
Throughout the years, WordPress has evolved greatly and has given new possibilities to users. To everyone ranging from individual bloggers, journalists, publishers to business owners and now to software developers from other open source communities. WordPress keeps on empowering its users. It continues to set an example of what people can achieve when they are driven by the same passion, yet coming from diverse backgrounds. Its REST API integration to the WordPress core, not only does it open up a new world of possibilities but it also creates a new world of complexity. This is what our managed WordPress hosting solution is really about, this is our contribution to the community. It is about enabling you, and your daily enterprise. It's about fighting off all that complexity, keeping everything running and humming and being there, when things, inevitably, will break.