Based on this HN discussion, which I discussed a while back, I looked at how to set up WP as a CRUD database accessible via API.
It wasn’t hard. Steps:
- Install WordPress (I used ec2 and the Cloudformation sample template)
- Install the following plugins
- I also installed the following optional plugins
- Json Basic Auth to protect the API during development
- Registered Users Only to protect the entire website
- I created a custom post type of ‘todo’ and added a couple of custom fields.
- I was able to get the todos by going to these URLs (apparently you can have the API live at wp-json, but that required some rejiggering of url permalinks).
- http://host/wordpress/?rest_route=/wp/v2/todo/8
- http://host/wordpress/?rest_route=/wp/v2/todos
Here’s an example of the output:
{ "id": 8, "date": "2018-03-05T02:38:26", "date_gmt": "2018-03-05T02:38:26", "guid": { "rendered": "http://host/wordpress/?post_type=todo&p=8" }, "modified": "2018-03-05T02:40:01", "modified_gmt": "2018-03-05T02:40:01", "slug": "auto-draft", "status": "publish", "type": "todo", "link": "http://host/wordpress/todo/auto-draft/", "title": { "rendered": "Buy Milk" }, "template": "", "acf": { "": false, "due_date": "20180308", "description": "please buy milk.", "who_owns_it": { "ID": "1", "user_firstname": "", "user_lastname": "", "nickname": "mooreds", "user_nicename": "mooreds", "display_name": "mooreds", "user_email": "...", "user_url": "", "user_registered": "2018-03-05 02:21:36", "user_description": "", "user_avatar": "..." }, "done": false }, "_links": { "self": [ { "href": "http://host/wordpress/wp-json/wp/v2/todo/8" } ], "collection": [ { "href": "http://host/wordpress/wp-json/wp/v2/todo" } ], "about": [ { "href": "http://host/wordpress/wp-json/wp/v2/types/todo" } ], "wp:attachment": [ { "href": "http://host/wordpress/wp-json/wp/v2/media?parent=8" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }
The custom post fields are all under the ACF key, and you can see that there was an expansion of the who_owns_it
field. If you are going to do this, make sure have the the normal title
tag be part of the custom post, otherwise the WP UX for editing the custom posts won’t be much use.
Not perfectly restful, but a super simple way to set up an API that non technical folks can use to create, update or delete records and that you can consume in other systems.