wp rest api - adding your own endpoint

16

Click here to load reader

Upload: keanan-koppenhaver

Post on 12-Jan-2017

259 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: WP REST API - Adding Your Own Endpoint

WP REST API: Adding Your Own Endpoint

Keanan Koppenhaver

http://levelupwp.net https://www.facebook.com/levelupwp

http://github.com/kkoppenhaver

Page 2: WP REST API - Adding Your Own Endpoint

Who Am I?

• WordPress Developer at doejo for 3 years

• Worked on WP sites large (XX,XXX posts) and small (5 simple pages)

• Have recently been using just pieces of the WP REST API in production

Page 3: WP REST API - Adding Your Own Endpoint

Why are we here today?

• Learn a little about the WP REST API

• See some real world examples

• Learn which pieces of the API are available in core

• Learn how to create our own endpoint

Page 4: WP REST API - Adding Your Own Endpoint

Questions?Ask them anytime in the chat box below.

Page 5: WP REST API - Adding Your Own Endpoint

How did the REST API come about?

• Proposed just after the WP Community Summit in 2012

• JSON over XML because of how it maps to programming language constructs

• REST because the REST paradigm matches up well with the CMS structure

• Merged* into Core on October 8, 2015 (https://core.trac.wordpress.org/changeset/34928)

Source: https://youtu.be/tV4IuS_KnvU?t=19m24s

Page 7: WP REST API - Adding Your Own Endpoint

Demo: ustwo.com

Page 8: WP REST API - Adding Your Own Endpoint

Demo: ustwo on Github

Page 9: WP REST API - Adding Your Own Endpoint

Is the WP REST API part of WP Core?

• Yes*

• Infrastructure is in core

• Prebuilt endpoints aren’t in core

• We can create our own endpoints without an additional plugin

• If we want all the endpoints we need a plugin

Page 10: WP REST API - Adding Your Own Endpoint

What does the plugin give us?

• Endpoints on posts, pages, revisions, media, post types, post statuses, comments, taxonomies, categories, tags and users

• Can be enabled for CPT or custom taxonomy

• For a full list, see the reference

Page 11: WP REST API - Adding Your Own Endpoint

And without the plugin?

• No endpoints available, but they can be created

• Step 1: Create a function to return the necessary data

• Step 2: Register with register_rest_route

• Step 3: Flush Rewrite Rules

• Step 4: Profit!

Page 12: WP REST API - Adding Your Own Endpoint

Step 1Create a function to return the necessary data

Page 13: WP REST API - Adding Your Own Endpoint

Step 2Register with register_rest_route

Page 14: WP REST API - Adding Your Own Endpoint

Step 3Flush Rewrite Rules

Page 15: WP REST API - Adding Your Own Endpoint

Step 4Profit!

Page 16: WP REST API - Adding Your Own Endpoint

Q&AKeanan Koppenhaver

http://levelupwp.net https://www.facebook.com/levelupwp

http://github.com/kkoppenhaver