Wordpress as a Backend

22
WordPress as a Backend WordCamp Dayton 2015 - Andrew Duthie

Transcript of Wordpress as a Backend

Page 1: Wordpress as a Backend

WordPress as a Backend

WordCamp Dayton 2015 - Andrew Duthie

Page 2: Wordpress as a Backend

About Me

• JavaScript Engineer at Automattic

• Co-Host of WordPress Cincinnati Meetup group

• Previously worked at a local agency building custom WordPress themes

Page 3: Wordpress as a Backend

Web Sites vs

Web Apps

Page 4: Wordpress as a Backend

Web Sites are documents

Page 5: Wordpress as a Backend

Web Apps are interactive

Page 6: Wordpress as a Backend

Speed Matters• With JavaScript, you have

the opportunity to render optimistically

• Even artificial content can give users the perception of speed

Page 7: Wordpress as a Backend

JSON REST API

Page 8: Wordpress as a Backend

JSONJavaScript Object Notation

{ "id": 1, "type": "post", "slug": "hello-world", "link": "http://wordpress.dev/hello-world/", "guid": { "rendered": “http://wordpress.dev/?p=1" }, "date": "2014-08-14T00:02:39", "modified": "2014-08-14T00:02:39", "title": { "rendered": "Hello world!” }, "content": { "rendered": "<p>Welcome to WordPress.</p>" }, "excerpt": { "rendered": "<p>Welcome to WordPress.</p>” }, "author": 1, "featured_image": 0, "comment_status": "open", "ping_status": "open", "sticky": false, "format": "standard" }

Page 9: Wordpress as a Backend

RESTRepresentational State Transfer

Action Endpoint HTTP Verb

List /posts/ GET

Read /posts/1 GET

Create /posts/ POST

Update /posts/1 PUT / PATCH

Delete /posts/1 DELETE

Page 10: Wordpress as a Backend

APIApplication Programming Interface

An API is an contract by which a programmer can interact with a

particular service

Page 11: Wordpress as a Backend

WordPress Data StoryXML-RPC API

• Verbose • Difficult to work with

XML-RPC JSON

<array> <data> <value><string>Hello</string></value> <value><string>World</string></value> </data> </array>

[ “Hello”, “World” ]

Page 12: Wordpress as a Backend

WordPress Data StoryWordPress.com / Jetpack REST API

• Simple to set up • Secure and mature • Requires Jetpack • Extra network hop

Page 13: Wordpress as a Backend

WordPress Data Storyadmin-ajax.php

• Ad hoc endpoints • Manually configured

Page 14: Wordpress as a Backend

WordPress JSON API

Page 15: Wordpress as a Backend

WordPress JSON API

• Alternative to XML-RPC • REST endpoints • Extendable • Expected later this year • Available now as a plugin

Page 16: Wordpress as a Backend

WordPress JSON APIQuerying

/wp-json/wp/posts?posts_per_page=10

http://codex.wordpress.org/Class_Reference/WP_Query http://wp-api.org/#posts_retrieve-posts

/wp-json/wp/posts?paged=2

Page 17: Wordpress as a Backend

WordPress JSON APIRegistering Endpoints

register_json_route( ‘my-plugin', '/actions', array( ‘methods' => WP_JSON_Server::READABLE, ‘callback' => array( $this, 'get_actions' ), ) );

register_post_type( ‘movie’, array( // … ‘show_in_json’ => true, ‘json_base’ => ‘movies’ ) );

Page 18: Wordpress as a Backend

WordPress JSON APIAuthentication

• Cookie Authentication • OAuth1 • Basic Authentication

http://wp-api.org/guides/authentication.html

Page 19: Wordpress as a Backend

WordPress JSON APIWP-API.js

• Official JavaScript library • Backbone models and collections • Transparent nonce handling

https://github.com/WP-API/client-js

Page 20: Wordpress as a Backend

Use Cases

• Single-page web apps • Mobile apps • Widgets • JavaScript themes • Other technology stacks

Page 21: Wordpress as a Backend

Demo

https://github.com/aduth/wordcamp-popular-movies

Page 22: Wordpress as a Backend

Thanks!

Questions?