Slide 1

Slide 1 text

An introduc on to the WordPress REST API Jack Lenox (@jacklenox) WordCamp Athens 2016

Slide 2

Slide 2 text

I Greece!

Slide 3

Slide 3 text

1. The case for a WordPress REST API

Slide 4

Slide 4 text

What is a REST API?

Slide 5

Slide 5 text

It stands for REpresenta onal State Transfer Applica on Programming Interface

Slide 6

Slide 6 text

Centred around verbs GET POST UPDATE PUT DELETE etc. Different ways to interact with your data

Slide 7

Slide 7 text

Usually responds in XML or JSON format XML (Extensible Markup Language) HTML RSS JSON (JavaScript Object Notation)

Slide 8

Slide 8 text

Why does WordPress need one?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

MoMA in New York (c. 2009) Their developers wanted to use: WordPress as the CMS Ruby to build the front-end

Slide 11

Slide 11 text

Why? I don't know... but they did

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

WordPress.com API Released in 2012 Available to self-hosted sites via Jetpack Powerful and road-tested Not extensible (unless you work at Automa c!)

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

REST API use cases Non-PHP implementations Mobile devices But most recently...

Slide 16

Slide 16 text

JavaScript OMGBBQ!

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Internet of things

Slide 19

Slide 19 text

So, what is the WordPress REST API?

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Merge meline WordPress 4.4 (8 December 2015) Infrastructure merged No endpoints WordPress 4.7 (due 6 December 2016) Endpoints merged!

Slide 23

Slide 23 text

2. How the WordPress REST API works (aka, what the heck is an endpoint?)

Slide 24

Slide 24 text

End‐what? REST APIs interprets a URL, and return a response Not wildly different to RSS Sample request to fetch some posts: wc-athens.dev/wp-json/wp/v2/posts

Slide 25

Slide 25 text

[ { "id": 1, "date": "2016-11-18T14:08:53", "date_gmt": "2016-11-18T14:08:53", "modified": "2016-11-18T14:08:53", "modified_gmt": "2016-11-18T14:08:53", "slug": "hello-world", "type": "post", "link": "http://wp-rest-api-demo.dev/2016/11/18/hello-wor "title": { "rendered": "Hello world!" }, "content": { "rendered": "

Welcome to WordPress. This is your fi "protected": false } } ]

Slide 26

Slide 26 text

More endpoints... Single post: wc-athens.dev/wp-json/wp/v2/posts/1 All pages: wc-athens.dev/wp-json/wp/v2/pages All users: wc-athens.dev/wp-json/wp/v2/users All categories: wc-athens.dev/wp-json/wp/v2/categories

Slide 27

Slide 27 text

Moar verbs! By default, our calls to the REST API are GET requests. But we can do a lot more: POST PUT UPDATE DELETE

Slide 28

Slide 28 text

Authen ca on!

Slide 29

Slide 29 text

A few ways to authen cate Basic Authen ca on Quick and dirty Cookie Authen ca on Good, especially for themes and plugins OAuth 1.0 A bit complicated, but versatile OAuth 2 Less complicated than OAuth 1, but requires HTTPS

Slide 30

Slide 30 text

Brokered Authen ca on The broker apps.wp-api.org Otherwise, it's OAuth 1.0

Slide 31

Slide 31 text

Modifying endpoints

Slide 32

Slide 32 text

3. Uses for the WordPress REST API

Slide 33

Slide 33 text

Custom wp‐admin interfaces Calypso developer.wordpress.com/calypso/ Happytables www.happytables.com

Slide 34

Slide 34 text

Mobile applica ons WordPress apps for iOS and Android apps.wordpress.com/mobile/ Vienna github.com/joehoyle/vienna

Slide 35

Slide 35 text

Themes Picard github.com/automattic/picard/ Anadama github.com/ryelle/anadama-react/ Foxhound github.com/ryelle/foxhound/

Slide 36

Slide 36 text

Plugins WooCommerce github.com/woocommerce/woocommerce EDD github.com/easydigitaldownloads/Easy-Digital- Downloads Syndication?

Slide 37

Slide 37 text

Enterprise Quartz qz.com Facebook facebookbrand.com Google IO (not WordPress, but...) events.google.com/io2016/

Slide 38

Slide 38 text

Very exci ng mes ahead!

Slide 39

Slide 39 text

Further reading, and any ques ons? The WordPress REST API docs v2.wp-api.org WordPress TV wordpress.tv (search "REST API") javascriptforwp.com

Slide 40

Slide 40 text

Ευχαριστώ πολύ! @jacklenox We're hiring! automa c.com/work‐with‐us/