Slide 1

Slide 1 text

KRISTARELLA.blog WP REST API (NOT) JUST ANOTHER … TALK

Slide 2

Slide 2 text

KRISTARELLA.blog @KRISTARELLA
 HAPPINESS ENGINEER: WP.COM KRISTEN SYMONDS

Slide 3

Slide 3 text

KRISTARELLA.blog (NOT) JUST ANOTHER WP REST API TALK OVERVIEW - What is a REST API? - What is the WP REST API? - A bit of history - Who is using it? - How to use it - What you can do with it - suggestions - examples

Slide 4

Slide 4 text

KRISTARELLA.blog REST API? WHAT IS A…

Slide 5

Slide 5 text

KRISTARELLA.blog WHAT IS A REST API? DEFINITIONS - Application Programming Interface - method for an application to talk to another app/service/language - REpresentational State Transfer - enables access & manipulation of a textual representation of a web-based resource - coined by Roy Fielding in his 2000 dissertation

Slide 6

Slide 6 text

KRISTARELLA.blog WHAT IS A REST API? CONSTRAINTS 1. Stateless
 Each request is independent of each other, and all information to complete the task needs to be included in one go 2. Cacheable 3. Uniform interface
 URI identifies the resource → method performs operation on resource → operation is implicit in the URL
 e.g., http://mybadass.blog/delete/product/coffee resource method

Slide 7

Slide 7 text

KRISTARELLA.blog WHAT IS A REST API? CONSTRAINTS 4. Client-server
 A separation of client & server where the server doesn’t care about the state of the server and the client doesn’t need to store information 5. Layered
 Doesn’t matter if the server is primary, or an intermediary 6. Response is in a hypermedia format (optional) - XML is a hypermedia format because it conveys meaning within the markup - JSON is sometimes - API author needs to structure output in a RESTful way

Slide 8

Slide 8 text

KRISTARELLA.blog WP REST API? WHAT IS THE…

Slide 9

Slide 9 text

KRISTARELLA.blog IMMEDIATELY TURNS YOUR WEB SITE INTO AN APPLICATION THAT CAN SERVE DATA TO ANY KIND OF APPLICATION OR LANGUAGE Topher Derosia WHAT IS THE WP REST API?

Slide 10

Slide 10 text

KRISTARELLA.blog WHAT IS THE WP REST API? 2009 MOMA NY uses WP back-end, Ruby front-end
 resulting in JSON API plugin Jan 2013 first code for WP REST API
 by Ryan McCue on Github Google SoC 2014 v1 stable: robust, but not ready for core
 (no internal API, difficult to extend) Feb 2015 started v2: Ryan McCue, Rachel Baker,
 Joe Hoyle, Daniel Bachhuber schemas posts, pages, media,
 terms, users controller, server Not to scale! Apr 2015 v2b1 May 2015 speed increase, custom
 response fields, pagination v2b2 Oct 2015 infrastructure merged into core! Jun 2013 Dec 2016 WP 4.7
 API merged into core!

Slide 11

Slide 11 text

KRISTARELLA.blog WHO IS USING IT? WP REST API

Slide 12

Slide 12 text

KRISTARELLA.blog - SearchWP - Tabulate - WooCommerce - EDD (not really) - ACF (with plugin)  WHO IS USING IT? PLUGINS

Slide 13

Slide 13 text

KRISTARELLA.blog SITES WHO IS USING IT?

Slide 14

Slide 14 text

KRISTARELLA.blog ADMIN WHO IS USING IT?

Slide 15

Slide 15 text

KRISTARELLA.blog WHO IS USING IT? MOBILE - Official Mobile App - https://apps.wordpress.com/mobile/ - Vienna - https://github.com/joehoyle/vienna

Slide 16

Slide 16 text

KRISTARELLA.blog HOW TO USE IT WP REST API

Slide 17

Slide 17 text

KRISTARELLA.blog WP-API.ORG DEVELOPER.WORDPRESS.ORG/REST-API

Slide 18

Slide 18 text

KRISTARELLA.blog HOW TO USE IT USING THE API 1. Find the URL you need - discovery URL is revealed in the head of your site 2. Construct the URL with resource and method 3. JSON reponse is returned 4. Parse information GET http://kristarella.blog/wp-json/wp/v2/posts

Slide 19

Slide 19 text

KRISTARELLA.blog HOW TO USE IT EXAMPLE RESPONSE [{ “id" 4357, “date":"2015-03-02T22:27:02", “date_gmt":"2015-03-02T11:27:02", “guid":{"rendered":"http:\/\/www.kristarella.com\/?p=4357"}, “modified":"2015-03-03T10:09:40","modified_gmt":"2015-03-02T 23:09:40", “slug":"seo-for-beginners", “type":"post", “link":"http:\/\/kristarella.blog\/2015\/03\/seo-for- beginners\/", "title":{"rendered":"SEO for beginners"}, http://kristarella.blog/wp-json/wp/v2/posts?search=css

Slide 20

Slide 20 text

KRISTARELLA.blog WHAT YOU CAN DO WITH IT IDEAS FOR YOU - On your site - suggest search terms for your visitors - pagination - archives/portfolio filter - replace admin-ajax.php tasks
 - Search a site from anywhere - streamline workflows - Quick post - Parse information into other APIs in any language - integrate signups with Salesforce, Meetup, Mailchimp - Display real-time sales data

Slide 21

Slide 21 text

KRISTARELLA.blog WHAT YOU CAN DO WITH IT CUSTOM FRONTEND APP $http
 .get("http://ilovetea.mystagingwebsite.com/wp-json/wp/v2/posts? per_page=100&orderby=title")
 .success(function(data) { $scope.drinks = data; });

Slide 22

Slide 22 text

KRISTARELLA.blog

Slide 23

Slide 23 text

KRISTARELLA.blog WHAT YOU CAN DO WITH IT SEARCH FROM ANYWHERE - Build your own search engine using Alfred workflows require './workflow.php';
 search( "{query}" );

Slide 24

Slide 24 text

KRISTARELLA.blog WHAT YOU CAN DO WITH IT: SEARCH FROM ANYWHERE function search_v2( $search ) { $num = 30; $fields = ‘URL,title'; $order = ‘asc'; $orderby = 'title'; $url = 'https://public-api.wordpress.com/wp/v2/sites/en.support.wordpress.com/pages/?order='. $order.'&orderby='.$orderby.'&per_page='.$num.'&search='.urlencode( $search ); $ch = curl_init(); curl_setopt( $ch, CURLOPT_FAILONERROR, false ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_TIMEOUT, 10 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_VERBOSE, true); $body = curl_exec( $ch ); $err = curl_errno( $ch ); $data = json_decode( $body, true ); if( !empty( $data ) ) { echo ''; while (list($key, $post) = each( $data ) ) { echo ''.$post['title']['rendered'].' '.$post['link'].' '; } echo ""; } }

Slide 25

Slide 25 text

KRISTARELLA.blog CREDITS - Images - https://www.flickr.com/photos/ swamibu/6818776091 - https://www.flickr.com/photos/ peter-trimming/15030180387

Slide 26

Slide 26 text

QUESTIONS? THANK YOU! KRISTARELLA.blog