Tools & Tech / April 2017

Using the WordPress REST API

Over the last decade, WordPress has evolved from a simple blogging platform to a robust content management system, providing flexible ways to design and build websites. In WordPress 4.4, the developers added a REST API into WordPress’ core code, making it easy to use the system for single page apps and highly interactive projects.

We had the opportunity to use the WordPress REST API for the West Philadelphia History Map with The People’s Emergency Center, so we could build an easy-to-update content management system with a completely custom front-end.

How it works

An application program interface, or API, is a communication protocol that allows a web browser or other client-side application to interact with a server. In the case of the REST API, WordPress provides a service to allow you to interface with the content that it stores, like posts, pages, users, taxonomies, media, or custom fields. You can interact with this API through HTTP requests to API endpoints, or routes. For example, you can make a request to GET all WordPress posts, and receive a JSON response that provides this information for you to use in your own project. Or, you could POST a new page to add it to your site from a custom form.

Screenshot of WordPress Rest API Setup

WordPress provides extensive documentation on the REST API online, and also explains helpful information on how to extend it further for your custom project. There is also a WordPress REST API (v2) plugin that aims to make building custom endpoints easy for users without programming experience, and several other plugins that extend the core features to integrate with other plugins or expand the API’s capabilities.

Building the West Philadelphia History Map

The West Philadelphia History Map is a project to document the rich history of West Philadelphia surrounding Lancaster Avenue from the early 17th Century through modern day. The project required an easy-to-use content management system that paired with an interactive digital map, which the WordPress REST API made possible.

Screenshot from WordPress REST API setup

We used the REST API to read information from the WordPress database for landmark information and the New Freedom Tour, a walking tour of African-American Islamic cultural heritage sites in West Philadelphia. We created a custom route that provides, or GETs, all of the landmarks with their required information, including title, description, latitude, longitude, time period, and category. We also created an endpoint to receive, in order, all tour stops and related content. This allows the managers of the map to maintain site content using WordPress’ user-focused administrative portal without being restricted to the WordPress templating engine.