43
Matt Perry // @mattoperry olope.me/wcsea17/slides.pdf WordPress REST API quick intro

WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

WordPress REST APIquick intro

Page 2: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

the basics extendinguse casesthe future

Page 3: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

PART 1

the basics

Page 4: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

REST APIrepresentational

state transfer

applicationprogramminginterface

Page 5: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

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

Page 6: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

JSON

Page 7: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

language of the REST API

JavaScript Object Notation

Page 8: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

PHP REST

functions/classesserver only localpageloadHTML

routes/endpointsclient/server

remoterequest/response

JSON

Page 9: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

routea uri that corresponds to an API operation

Page 10: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

ROUTE: EXAMPLE

wp-json/wp/v2/posts

basepath

namespace

resource path

version

Page 11: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

route + http method = endpoint

Page 12: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

/wp-json/wp/v2/posts

Page 13: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

FUN FACTS

100s of endpoints

on by default

everywhere! (4.7+)

Page 14: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

Page 15: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

Page 16: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

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

Page 17: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

developer.wordpress.org/rest-api

Page 18: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

PART 2

extending

Page 19: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

init parse_request

rewrite rules query var

rest_api_loaded()

Page 20: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

Page 21: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

CHANGING BUILT-IN ENDPOINTS

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

Page 22: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

CHANGING BUILT-IN ENDPOINTS

register_meta( $type, $key, $args )

Page 23: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

CHANGING BUILT-IN ENDPOINTS

Page 24: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

CHANGING BUILT-IN ENDPOINTS

Page 25: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

CHANGING BUILT-IN ENDPOINTS

Page 26: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

CUSTOM CONTENT TYPES

post, taxonomyhidden by defaultwp namespace

Page 27: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

CUSTOM CONTENT TYPES

Page 28: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

MAKING BRAND NEW ENDPOINTS

pluginsnamespaces

wp-json/myplugin/v1/…

Page 29: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

MAKING BRAND NEW ENDPOINTS

register_rest_route()

rest_api_init

Page 30: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

MAKING BRAND NEW ENDPOINTS

Page 31: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

MAKING BRAND NEW ENDPOINTS

Page 32: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

MAKING BRAND NEW ENDPOINTS

Page 33: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

MAKING BRAND NEW ENDPOINTS

Page 34: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

Page 35: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

PERMISSIONS AND AUTHENTICATION

permissions_callback

cookie auth / nonces

plugins: OAuth1, basic …

Page 36: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

PART 3

use cases

Page 37: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

USE CASES

Page 38: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

USE CASES

Time Magazine Quartz

Facebook Branding Guggenheim

Page 39: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

USE CASES

content networks

wp wp wp wp wp wp

contentstore

Page 40: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

USE CASES

native appsimport / exportrealtime appsintegrations

Page 41: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

THE FUTURE

WP: “restification”a universal language

Page 42: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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

NEXT STEPS

learn“restify” your plugin

contribute

Page 43: WordPress REST API - WordCamp Seattle 2017...olope.me/wcsea17/slides.pdf Matt Perry // @mattoperry ALL ENDPOINTS developer.wordpress.org/rest-api/reference GET/wp-json posts, post

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