WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry...

Preview:

Citation preview

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

WordPress REST APIquick intro

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

the basics extendinguse casesthe future

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

PART 1

the basics

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

REST APIrepresentational

state transfer

applicationprogramminginterface

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

A interface that uses http operations (GET, POST …) to interact with an application.

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

JSON

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

language of the REST API

JavaScript Object Notation

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

PHP REST

functions/classesserver only localpageloadHTML

routes/endpointsclient/server

remoterequest/response

JSON

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

routea uri that corresponds to an API operation

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

ROUTE: EXAMPLE

wp-json/wp/v2/posts

basepath

namespace

resource path

version

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

route + http method = endpoint

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

/wp-json/wp/v2/posts

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

FUN FACTS

100s of endpoints

on by default

everywhere! (4.7+)

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

ENDPOINT EXAMPLES

GET /wp-json/wp/v2/posts?per_page=5&author=1

POST /wp-json/wp/v2/categories

DELETE /wp-json/wp/v2/tags/1

POST /wp-json/wp/v2/categories/5

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

ALL ENDPOINTS

developer.wordpress.org/rest-api/referenceGET/wp-json

posts, post revisions, categories, tags, pages, comments taxonomies, media, users, post types, post statues, settings

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

plugin 4.4 4.5 4.6 4.7 4.8 2013 ——————————— — — — — — — — 2016 2016 2017

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

developer.wordpress.org/rest-api

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

PART 2

extending

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

init parse_request

rewrite rules query var

rest_api_loaded()

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

REST_API_LOADED()

REST ROUTE? —> rest_get_server()

WP_REST_Server serve_request()

WP_REST_Request

dispatch()

WP_REST_Response

rest_api_init

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

CHANGING BUILT-IN ENDPOINTS

:) add data :( remove data :( modify data

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

CHANGING BUILT-IN ENDPOINTS

register_meta( $type, $key, $args )

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

CHANGING BUILT-IN ENDPOINTS

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

CHANGING BUILT-IN ENDPOINTS

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

CHANGING BUILT-IN ENDPOINTS

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

CUSTOM CONTENT TYPES

post, taxonomyhidden by defaultwp namespace

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

CUSTOM CONTENT TYPES

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

MAKING BRAND NEW ENDPOINTS

pluginsnamespaces

wp-json/myplugin/v1/…

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

MAKING BRAND NEW ENDPOINTS

register_rest_route()

rest_api_init

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

MAKING BRAND NEW ENDPOINTS

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

MAKING BRAND NEW ENDPOINTS

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

MAKING BRAND NEW ENDPOINTS

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

MAKING BRAND NEW ENDPOINTS

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

MAKING BRAND NEW ENDPOINTS

sanitization/validation/more

WP_REST_Controller https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#the-controller-pattern

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

PERMISSIONS AND AUTHENTICATION

permissions_callback

cookie auth / nonces

plugins: OAuth1, basic …

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

PART 3

use cases

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

USE CASES

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

USE CASES

Time Magazine Quartz

Facebook Branding Guggenheim

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

USE CASES

content networks

wp wp wp wp wp wp

contentstore

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

USE CASES

native appsimport / exportrealtime appsintegrations

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

THE FUTURE

WP: “restification”a universal language

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf

NEXT STEPS

learn“restify” your plugin

contribute

Matt Perry // @mattoperryolope.me/wcsea17/slides.pdf