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
- It allows themes to be built either in part or potentially in whole as single page applications.
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.
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.
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.