The Wordpress REST API

A presentation at Clique Studios Internal in April 2019 in Chicago, IL, USA by Carly Ho

Slide 1

Slide 1

The Wordpress REST API and getting the most out of it

Slide 2

Slide 2

Let’s get on the same page

Slide 3

Slide 3

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.

Slide 4

Slide 4

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

Slide 5

Slide 5

What’s JSON? JavaScript Object Notation; a textual format for storing and parsing javascript objects Many APIs return their payload formatted in JSON.

Slide 6

Slide 6

The Wordpress API

Slide 7

Slide 7

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

Slide 8

Slide 8

Hey, look at that.

Slide 9

Slide 9

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!

Slide 10

Slide 10

The start of the page data.

Slide 11

Slide 11

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

Slide 12

Slide 12

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

Slide 13

Slide 13

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

Slide 14

Slide 14

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?

Slide 15

Slide 15

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’ ));

Slide 16

Slide 16

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.

Slide 17

Slide 17

Now you too can REST Full documentation at https://developer.wordpress.org/rest-api