Don’t Worry, Be API

I have been battling with the WP REST API for a bit over a month now. There have been a few snags mostly to do with a lack of documentation but also due to a couple of issues that have slowed me down.

So that there is something out there to help the REST of the world I am putting out my own examples to show that once you have a proper handle on it this can be a very powerful tool indeed. The examples and tutorials I will be building will be split into two main sections being WP internal and external. This is primarily because I suspect that this is the way that the api will be used and evolve. Furthermore I will try to give practical examples of why you would do something like this rather than just use the existing WordPress infrastructure.

I will also be using the tags and categories system built into WordPress so that you can track down the articles you are interested in.

I will also be trying to extend this to external resources since there is and will be a lot of good stuff out there. The unfortunate thing is that there is also a lot of useless stuff. I intend to try and sort the wheat from the chaff. I am also working on some ideas of how I can allow other people’s work to be automatically included. This is part of an existing project I am doing so I hope it won’t be too long before I can offer this to other bloggers.

Internal WP Uses

The REST API opens up WordPress theme development in two major ways

  1. It allows themes to be built either in part or potentially in whole as single page applications.
  2. It opens up theme development to people who are more versed in Javascript than  the idiosyncratic WordPress methods – lets call this breaking out of the loop.

When looking at traditional theme development we are used to seeing a complete theme being deployed. whilst this could be done using the REST API right now as a single page app there are two main issues. First is that there are potentially a lot of elements to a WordPress page that need to be accounted for and best practice for doing this does not yet exist. For example one of the key strengths of WordPress has been that it has been easy for non code type developers to dive in and produce good results. We need to create an environment where this continues to be the case. In particular whilst we could happily use the style.css file in the initial load of a single page theme, I have not seen any equivalent to the functions.php file for adding your own code in a simple way – although I do have an idea which will form the basis of another article should a good alternative not materialise beforehand.

The second thing is that there is a fly in the ointment of a single page theme and that is non javascript enabled clients. Whilst these may be small in percentages of site visitors, they could well be significant. Search engines are getting there with following javascript links and there are those folks who will turn of javascript. Should you need to make sure that these are catered for then you need a method for handling this and for this purpose isomorphic javascript systems have been developed. Unfortunately WordPress is not there yet with this.

So what would I use the api for internally if not for the theme? Well what the REST api does is allow you to think of the WordPress as just that, data and data that you can manipulate to your hearts content without the need for a complete page refresh. Right off the top of my head the first thing could be comments. These could be submitted without a page refresh and if there were a lot of them there could be an efficient view more feature. Likewise for posts that span multiple pages, don’t load an entire new page, just get the content. These are for the standard things we do right now but when you extend it to more complex data such as we have in our Powwows system then these are turned from a really clunky beast into a smooth sharp tool.

Ultimately though I think that we will see the end of themes as we currently know them and this is a good thing. When components like these described are developed out then we will not need to have the often strange incantations of the WordPress php functions, hooks and filters to operate. A more complete theme with it’s own hooks and filters based on Javascript callbacks seems to me to be far preferable and far more industry standard these days, there are even a whole host of libraries out there for this very purpose such as react.js, angular.js etc..

External Uses

This is the world’s your oyster time. This allows connectivity for data flow in both directions to external apps and services.

I intend to show how you would do this and extend it to example system for:

  • A small business app to co-ordinate comms with customers, use the api.
  • Import a large volume of data from external feeds

Next Post: Getting Started

The REST aPI is one where there is a little setup to be done so I will guide you through this.

Leave a Reply