A presentation at Clique Studios Internal in April 2019 in Chicago, IL, USA by Carly Ho
The Wordpress REST API and getting the most out of it
Let’s get on the same page
What’s an API? Application Programming Interface It’s a way for applications to communicate with each other, or for discrete parts of an application to communicate with each other.
What’s REST? REpresentational State Transfer ● ● ● Behind a lot of operations on the Web You probably use GET and POST a lot with forms Submit request, get response with payload
What’s JSON? JavaScript Object Notation; a textual format for storing and parsing javascript objects Many APIs return their payload formatted in JSON.
The Wordpress API
What’s already there ● ● ● Find the API by going to /wp-json (e.g. chicagowatertaxi.com/wp-json) This will get you the site info and a list of API routes you can use Some of these routes will be generated by plugins, which often have an API for internal utility purposes
Hey, look at that.
Let’s look at some data If you look at /wp-json/wp/v2/posts, you’ll find a JSON object that returns data for all posts (for most of our sites this will not return very much, because we don’t usually use the ‘post’ post type) /wp-json/wp/v2/pages is also available, and probably has a lot more to look at. Try it on a site!
The start of the page data.
Performing actions Routes also have different REST endpoints based on the method used to access them (e.g. GET, POST, or DELETE) E.g., if you go to the route for an individual post with the correct credentials, using GET will get the contents, POST will update the contents, and DELETE will delete the post
Why might we use this? ● ● ● Using Wordpress as a “headless” backend with Vue/React/etc. Creating a progressive web app where we need to navigate pages without reloading the page Loading more posts asynchronously via a “load more” button
Let’s make it a little more useful Raise your hand if you’ve created a custom post type for a project? An option to keep in your head: ‘show_in_rest’ This is always initially set to false, but will allow you to use /wp-json/wp/v2/post_type
Let’s make it even MORE useful? That’s great, but: ● ● ● What if I want the content of the custom fields, too? What if I want to structure the return content in a different way? What if I want to return something other than JSON?
Using register_rest_route Setting up a route: register_rest_route(‘namespace/v1’, ‘route’, array( ‘methods’ => WP_REST_SERVER::READABLE, ‘callback’ => ‘your_handler_function_name’ ));
Adding as an action Then add the action to the REST API initializer: add_action( ‘rest_api_init’, ‘initializer_function_name’ ); You’ll have to flush your permalinks, but once you do you should be able to navigate to /wp-json/namespace/v1/route and get a response based on what you put in your handler.
Now you too can REST Full documentation at https://developer.wordpress.org/rest-api
View The Wordpress REST API on Notist.
Dismiss
A brief overview of the Wordpress built-in REST API and how to extend it, plus a tour through what all those acronyms mean.