A Look At WordPress' Rest API | Duckpin Blog

Web Development

A Look at WordPress’ Rest API

October 24, 2017 by Ryan Dunn

Web developer, Ryan Dunn, recently attended Baltimore Word Camp, an annual professional event focused on the content publishing platform WordPress. Read on to find out about his takeaways and their implications for website management.

WordPress’ open source community made a monumental win when the CMS officially supported the WP REST API in their 4.7 core update. I came across this gem when I was at the Baltimore Word Camp ’17, while listening to Ethan Butler’s talk simply labeled ‘Write Better Javascript’. During the talk he was going through best practices in code structure and utilization of ES6 principles, but he kept showing code that was looping through posts in Javascript. I thought to myself, “WordPress doesn’t allow for this. PHP is the language of WordPress, but he is using Javascript to do everything. How is this happening?!”. After the talk, I asked him how this magic was happening, and he was using the WP REST API, and his goal was to never touch PHP again. He is a Javascript developer that recognizes WordPress as a powerful and familiar backend CMS. My mind was blown.

So the WP REST API allows any WordPress site that is updated to version 4.7+ to be able to remotely retrieve JSON data for just about every item that you would want to output on a normal site. The most basic retrieval method is to send a GET request to http://www.yoursite.com/wp-json/ and the response is your site’s posts, pages, media, etc. formatted in JSON. There are many endpoints to narrow down your requests. More information about the details can be found in the REST API Handbook.

Javascript

As I mentioned above, this is a big win for Javascript/WordPress developers. This technology allows Javascript to play a bigger role in a WP dev environment. Whether it be a part of a page, a whole page, or a full website run by javascript on the front end and WordPress in the back end, we can now have that opportunity and that tool in our tool belt. WordPress supports a Javascript Client for client side standard js and server side node.js as well, so you don’t have to create looping functions and other standard functions you may need to use it properly. I am personally excited to try this tech out in conjunction with Vue.js to utilize the Virtual DOM reactiveness, even if it just in part of a page. If you haven’t heard of Vue, it is similar to React, but it uses HTML as the templating language, not JSX, which allows for integration in a less niche dev environment. As time progresses, it’s going to be more and more necessary to hone in on your javascript skills as a WordPress developer, and this is just another reason why.

Other Uses

But, javascript is not the only reason to get excited about the WP REST API. Since its response is formatted in JSON, it works with a plethora of other languages. It has a recommended Ruby client, and I’m sure there are clients for other languages out there. It also allows you to display WP information on other PHP sites that aren’t WordPress based. Suppose you have a client that has an old site that you inherited, but they want a blog on it. In most cases, you can now install wp and maintain the back end it on your server, and use the REST API to embed the information on the client’s front end for a more seamless UX integration. Having this tool enhances the reach that WordPress has across the web, and enables it to be more inclusive to other languages and dev environments, which has always been a major goal of the WordPress project.

“I believe quite strongly that JavaScript and API-driven interfaces are the future of not just WordPress but the web” – Matt Mullenweg – WordCamp US 2015 – Philadelphia PA

Summary

WordPress is always going to be open source and a community driven project. Anyone can jump in and participate in core updates and make plugins to extend its capabilities. This REST API was actually at first a plugin created by someone that had a bright idea and the perseverance to complete it. That plugin got adopted into the core in 2015 not just because the WP team wanted it, but because it was being used by the community so much. The WP team listened and adopted. This is one of the many ways that the WordPress community has broken the mold of the traditional WordPress setup and new ideas are in constant development by the community. The WordPress community drives its future, not its internal developers. The trend that I’ve been seeing is that people love its familiar backend dashboard and the MySQL DB structure, but are doing more and more to change how the front end is structured to streamline development and incorporate DRY (Don’t Repeat Yourself) principles. And WordPress is constantly listening.

Ready to get a project rolling?Let's Get Started

Ready to get a project rolling?Let's Get Started