Tonight I gave a presentation at the Tunbridge Wells WordPress Meetup about the WordPress REST API. I called it an “absolute beginners guide” because before looking at it for the presentation I had never looked at it previously, so I was an “absolute beginner” with it.
The WordPress REST API was included in the WordPress core in version 4.7, meaning the WordPress development team consider it complete and stable and ready for production use.
Here is the slidedeck from the presentation:
- Been using WordPress for around 6 months.
- I never used the WordPress REST API.
- Do however have 17+ years of development experience.
- The WordPress REST API was added to core in version 4.7.
- Previously available as a plugin, but now considered complete and stable.
- Full documentation available at http://v2.wp-api.org
- Requests to the REST API go to URLs under this location.
- An HTTP GET request to this location will get you a list of posts, JSON encoded.
- Example https://www.thesun.co.uk/wp-json/wp/v2/posts
- You can add additional parameters to filter the responses.
- Get posts with the terms “awesome” in them.
- Limit to pages and posts per page.
- This will get just one post using the Post ID.
- To make other types of requests, e.g. create posts, delete posts, etc…you need to be authenticated.
- There are three types of authentication available:
- Cookie – Used for plugin and theme developers, as you need to have a logged in user.
- OAuth – Used by external applications
- Password – Strongly discourage for production use, but can be useful in developments (and demos)
- OAuth and Password require an additional plugin
- An HTTP POST request to the same address will create a new post, but you have to be authenticated and pass the correct post fields.
- An HTTP POST request with a Post ID specified will update the existing post with the data that is passed.
- A DELETE HTTP request with a Post ID specified will delete the post.
- Optionally you can specify “force=true” to delete the post completely and not just move it to the bin.
- Via the REST API you can do pretty much anything you can do via the normal WP Admin.
- Plugins can extend the REST API and add data to it or additional endpoints so you can access their data and functions.
- There are many use cases, but this brings WordPress into the realm of a fully fledged enterprise level Content Management System.