Getting started with the

WP REST API

- Aaron Rutley

Tonights talk:

Introduce the REST API and explain some practical ways you could get started using it in your projects!

  • Alternative way to communicate with the WordPress database
  • Coming in WordPress 4.7 (Early December 2016)

Acronyms

  • JSON - JavaScript Object Notation
  • REST - Representational State Transfer
  • API - Application Programming Interface

Developer friendly, well structured way to talk to the WP database.

The REST API is all about endpoints

http://aaronrutley.com/wp-json/wp/v2/

Getting data from the REST API

To GET a collection of posts

http://aaronrutley.com/wp-json/wp/v2/posts

To GET an individual post

http://aaronrutley.com/wp-json/wp/v2/posts/3556

To GET page two of posts

http://aaronrutley.com/wp-json/wp/v2/posts?per_page=10&page=2

Posting data to the REST API

POST data to create a new pages

http://aaronrutley.com/wp-json/wp/v2/pages

PUT data to update a page

http://aaronrutley.com/wp-json/wp/v2/pages/4

* Authentication Required

Default Content Endpoints

  • Posts/Pages/Media
  • Comments
  • Terms
  • Users
  • Meta

Custom Post Type Endpoints

When registering a CPT

'show_in_rest' => true,

To GET a collection of 'projects'

http://aaronrutley.com/wp-json/wp/v2/projects

To GET a customised collection of 'projects'

http://aaronrutley.com/wp-json/wp/v2/projects?orderby=title&order=asc
-->

Simple ways to use the WP REST API

  1. Load More
  2. Category Filtering
  3. Loading data from an external site

Demo Time

Tips for getting started with the REST API

Download & Install the WP REST API plugin

Use chrome with the JSON Formatter Extension

Use the WordPress REST API Console Plugin

Checkout Paw https://paw.cloud/

Try Postman app https://www.getpostman.com

To recap - why is this a big deal ?

  • It can be used right now to improve your existing projects!
  • It can be used in core/plugins to improve the wp-admin (Quick Draft)
  • With authentication we can use it to create Custom Dashboards
  • We can now use WP as Headless CMS (Data store for an iOS App)
  • It can be used to create Single Page Applications (React, Angular, Vue)