Creating my first WordPress API

I recently started a website project to encourage the communities of Devon to be more involved in the services that we deliver in their community. We have four schemes that in principle help communities to help themselves. These schemes include Snow Wardens, Road Warden, Community self help and Parish Path Partnership (P3). We want to get more residents of Devon involved in these schemes.

One way of doing this is a postcode search of their area and a query returning which schemes are running in the area and how to get involved.

I started this by bring a number of sources of data together. I decided to base this on the local parish and town councils, so i imported the data from our PinPoint Devon community service system (https://www.pinpointdevon.co.uk/) that holds name, address, and asscoiated information.

By way of a challenge, not every parish has a parish council, so I had to undertake a further step to map the parishes that parish council are in. I added the spatial information and neighbouring parishes from Ordnance Survey database.

I now have all the data sitting in a WordPress custom post type called parishes. I then developed a widget to display this information by entering a postcode.

Normally this would be the end of the story: a customer facing postcode search that will return their parish council, neighbouring parish council and whether the council is in the scheme that they are looking at – or not.

Enter the WordPress REST API

In the latest version of WordPress 4.7 they have introduced a fully working API that will issue data on request in the standard JSON format. One of the nice things about WordPress is the ability to expand it to do more.

Like with most WordPress functionality it is quite straight forward to extend existing custom post types to display using the API. So I added a couple of lines of code to the register post type hook for the parishes custom post type to make it visible to the API.

Now, if I make an API request to WordPress for my custom post type of parishes I see the standard content of a post. This is great but it only gives me the parish council title and nothing more.  The API comes with a range of functions to expand it and include custom fields. I registered my custom fields into the API and now i have a functioning parish council API that can be queried against to import into other systems.

The beta address for the API is below and as you can see it uses the basic WordPress REST API functionality but with a new endpoint of parish-api.

https://beta.devon.gov.uk/community/wp-json/wp/v2/parish-api

You can run all the standard WordPress API queries on it to retrieve the data you require.

Footnote – as this process to expand the custom post type into a working API is quite straight forward, I expect I will be using it for more of our content to open up our website data.

Tim Barrett

I am a frontend developer for Devon County Council. I work mainly on WordPress and PHP.

More Posts

Follow Me:
Google Plus

Be the first to Comment

Your email address will not be published. Required fields are marked *

Comments are held for moderation. House rules