WordPress, API, JSON – POC for a small Rest API with WordPress mobile applications

Still looking for a simple way to handle a POC to create a straightforward and simple API with a minimum of work.

I had 2 main objectives for this POC like in the previous post:

  1. This API should populate data for mobile application so I must be able to minimize/shrink the values exposed in the json output.
  2. As it is a POC, my purpose is to be totally autonomous. I do not want to rely on developers in the conception and creation of this API.

Why should I reinvent the wheel ? So why do not leverage on WordPress and especially since the version of WP 4.4, WP has a powerful API.
For those, who know already WP, I will just indicate what I will try to make the best of WP using : Post Type, Taxonomy, Custom Fields… etc.

I had in mind this time not an API to manage translations but an API to handle Urban Art Spots such as the website Urbacolors.

The WP REST API ecosystem

Here is the list of plugin that will enhance and ease your experience within the WP REST API.

I have tested them all, according to my needs, here is my feedback on each of them.

  1. These 3 plugins : REST API Log, WP Rest Routes, REST API Console are essential. If you want to work with the WP API, it is required.
  2. ACF to REST API: in my opinion, you’ll better use this plugin instead of ACF to WP API. It goes as a kind of “bundle” with the ACF (Advanced Custom Fields) Plugin for WordPress. Very powerful if you intend to use Custom Fields within your API.
  3. WP REST API: when you are using WP 4.4 and more, it is useless to install this plugin.
  4. WP REST API Cache: when you are in the development phase, it is useless to work with a cache because you can not see immediately the changes in JSON format for instance. In production, you can use it for sure.

Make you own plugin

The thing is that it is very easy to create your own plugin within WP especially if you want to use post_type. That is the best to do. It is a main purpose of my plug-in : managing 5 post_type with a custom and common taxonomy.
For some purpose, I did also some Custom Fields but believe me, better use the ACF plugin for POC at least and making Custom Fields, with the help of ACF (Advanced Custom Fields) plugin is a piece of cake.

I have made 2 “uncompleted” but sufficient plugins for the POC.
You can find on github @
Look for bf_poc_api_wp in https://github.com/bflaven/PluginWordpressForFun

  1. Urban Art Hunting App
  2. WP REST API Urban Art Hunting App

Few Web API Endpoint Reference for WP API

<!-- main Endpoint -->
http://[path-to-your-worpress]/wp-json/wp/v2/
<!-- Posts Endpoint -->
http://[path-to-your-worpress]/wp-json/wp/v2/posts
<!-- etc... -->

If you declare for each post_type 'show_in_rest' => true,, automatically the WP API will show up your post_type.
You will get 2 routes with the same methods as you will have for the posts

For instance, our post_type is named locations
Show all the locations

wp/v2/locations
...
methods": [
        "GET",
        "POST"
      ],

All methods for each locations record

wp/v2/locations/(?P<id>[\\d]+)
...
"methods": [
        "GET",
        "POST",
        "PUT",
        "PATCH",
        "DELETE"
      ],

Some of the routes that automatically created by WP

Part 1 : The routes of the WordPress API
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

Part 2 : The routes of the WordPress API
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

REST API plugins

This Plugin WP Rest Routes is great. It will enable you to manage the routes of your API and the data displayed for each route. It is a true vital plugin.
In my opinion, the other 2 plugins (REST API Log, REST API Console) are useful but not vital.

Plugin WP Rest Routes – Part 1
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

Plugin WP Rest Routes – Part 2
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

Plugin WP Rest Routes – Part 3 – JSON output from the route get_locations defined in the Plugin WP Rest Routes
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

The input of our previous screens capture inside the admin of WP, our post_type is named locations
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

The REST API Log useful to get data on traffic and on API’s usage
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

The REST API Console plug-in that simply list the endpoints. Not bad
Worpress, API, JSON - POC for a small Rest API with WordPress mobile applications

Conclusion:
WP API has several advantages. I will stress 2 of them.

  1. Using WP gives the opportunity to anyone to create content that will be displayed in the API. It seems obvious but when you are working with people specialized for instance in Art, Literature or in any area. It is better to let them create the content to avoid any mistakes. Focus on the container and not on the content should be your motto.
  2. With the help of the Plugin WP Rest Routes, you have the ability to design the architecture quickly with all the routes of your future API. Thus, it will give you a living example for your future development with more experienced developers. According to the proverb : “A picture is worth a thousand words”.

Learn More