wp rest api - adding your own endpoint

Post on 12-Jan-2017

259 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

WP REST API: Adding Your Own Endpoint

Keanan Koppenhaver

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

http://github.com/kkoppenhaver

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

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

Questions?Ask them anytime in the chat box below.

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

Demo: ustwo.com

Demo: ustwo on Github

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

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

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!

Step 1Create a function to return the necessary data

Step 2Register with register_rest_route

Step 3Flush Rewrite Rules

Step 4Profit!

Q&AKeanan Koppenhaver

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

http://github.com/kkoppenhaver

top related