Slide 1

Slide 1 text

Put Your Content to REST with WP API Rachel Baker @rachelbaker WC Chicago 2014

Slide 2

Slide 2 text

WP API: JSON-based REST API for WordPress http://github.com/WP-API/WP-API http://wordpress.org/plugins/json-rest-api

Slide 3

Slide 3 text

Ryan McCue and Contributors

Slide 4

Slide 4 text

Coming to WordPress 4.1

Slide 5

Slide 5 text

WP-API: Soon to be available to ~22% of the web.

Slide 6

Slide 6 text

•Posts •Pages •Custom Post Types •Post Meta •Media •Comments •Taxonomies •Users •….or extend as needed WP API: Create, Read, Update, and Delete

Slide 7

Slide 7 text

Meh.

Slide 8

Slide 8 text

API: Application Programming Interface • Specifies how software components should interact with each other. • In relation to SOAP and REST services, an API is a specification of remote calls exposed to the API consumers.

Slide 9

Slide 9 text

REST: Representational State Transfer • A set of specifications and architecture styles/ constraints for web services. • Resources (nouns) are assigned unique URIs. • Operations are specified by HTTP methods (verbs) such as GET, POST, PUT, DELETE, etc. • Can be used wherever HTTP can. • Alternative to more complex protocols such as SOAP or RPC.

Slide 10

Slide 10 text

REST: Representational State Transfer example.com/api/animals/ example.com/api/animals/1/ example.com/api/animals/ GET GET POST example.com/api/animals/1/ PUT example.com/api/animals/1/ DELETE Resource
 (noun) Operation
 (verb)

Slide 11

Slide 11 text

JSON: JavaScript Object Notation • An open standard format that uses human- readable text to transmit data objects consisting of attribute–value pairs. • Alternative to Extensible Markup Language (XML) targeted at data- interchange. • Code for parsing and generating JSON data is readily available in a large variety of programming languages.


Slide 12

Slide 12 text

Doesn’t WordPress already have API?

Slide 13

Slide 13 text

• Cookie-based authentication • For themes or plugins performing client-side operations. • Requires nonces for manual Ajax requests. • Built into the core plugin. • Basic authentication • For development of external clients. • Requires Basic Auth plugin. • OAuth 1.0 authentication • For secure communication of external clients. • Requires OAuth plugin XML-RPC API

Slide 14

Slide 14 text

• Cookie-based authentication • For themes or plugins performing client-side operations. • Requires nonces for manual Ajax requests. • Built into the core plugin. • Basic authentication • For development of external clients. • Requires Basic Auth plugin. • OAuth 1.0 authentication • For secure communication of external clients. • Requires OAuth plugin WordPress.com JSON REST API

Slide 15

Slide 15 text

http://github.com/ WP-API/WP-API/ blob/master/docs/ comparison.md

Slide 16

Slide 16 text

Still I must ponder how the WP API could be useful to me?

Slide 17

Slide 17 text

•Backbone.js themes or plugins
 ! •Receiving and populating content for “infinite scrolling”
 ! •Form validation and submissions
 ! •Front-end content editing WP API Use Cases: Perform CRUD operations on the client-side

Slide 18

Slide 18 text

•Display content from another site within a multisite network.
 ! •Display content from a separate WordPress site. ! •Create or update content on a separate WordPress site or another site within a multisite network. WP API Use Cases: Perform CRUD operations on another WordPress site

Slide 19

Slide 19 text

•Display and manage your content in a mobile application. ! •Integrate your content with other application platforms.
 ! • Integrate other application platforms with your WordPress site. WP API Use Cases: Break your content free from WordPress

Slide 20

Slide 20 text

Interesting… How would I go about using 
 WP API?

Slide 21

Slide 21 text

• Download the plugin from Github or the WordPress.org plugin repository. • Github: http://github.com/WP-API/WP-API • WP.org: http://wordpress.org/plugins/json-rest-api ! • Activate the plugin. ! • Verify your permalink settings are NOT set on default. Get Started Now

Slide 22

Slide 22 text

• Cookie-based authentication • For themes or plugins performing client-side operations. • Requires nonces for manual Ajax requests. • Built into the core plugin. • Basic authentication • For development of external clients. • Requires Basic Auth plugin. • OAuth 1.0 authentication • For secure communication of external clients. • Requires OAuth plugin Documentation

Slide 23

Slide 23 text

/wp-json/ GET

Slide 24

Slide 24 text

/wp-json/posts GET

Slide 25

Slide 25 text

/wp-json/taxonomies GET

Slide 26

Slide 26 text

/wp-json/media GET

Slide 27

Slide 27 text

/wp-json/posts/1/comments GET

Slide 28

Slide 28 text

/wp-json/users GET GET /wp-json/posts/1/meta

Slide 29

Slide 29 text

• Cookie-based authentication • For themes or plugins performing client-side operations. • Requires nonces for manual Ajax requests. • Built into the core plugin. • OAuth 1.0 authentication • For secure communication of external clients. • Requires OAuth plugin • Basic authentication • For development of external clients. • Requires Basic Auth plugin. Authentication Options

Slide 30

Slide 30 text

• Cookie-based authentication • For themes or plugins performing client-side operations. • Requires nonces for manual Ajax requests. • Built into the core plugin. • Basic authentication • For development of external clients. • Requires Basic Auth plugin. • OAuth 1.0 authentication • For secure communication of external clients. • Requires OAuth plugin WP API OAuth1 http://github.com/WP-API/OAuth1

Slide 31

Slide 31 text

• Cookie-based authentication • For themes or plugins performing client-side operations. • Requires nonces for manual Ajax requests. • Built into the core plugin. • Basic authentication • For development of external clients. • Requires Basic Auth plugin. • OAuth 1.0 authentication • For secure communication of external clients. • Requires OAuth plugin WP API Basic Auth http://github.com/WP-API/Basic-Auth

Slide 33

Slide 33 text

Response {! ! "ID":6,! ! "title":"Put Your Content to REST with WP API",! ! "status":"publish",! ! "type":"post",! ! "author":{! ! ! "ID":1,! ! ! "username":"admin",! ! ! "name":"admin",! ! ! "first_name":"",! ! ! "last_name":"",! ! ! "nickname":"admin",! ! ! "slug":"admin",! ! ! "URL":"",! ! ! "avatar":"http:\/\/1.gravatar.com\/avatar\/7c4ff521986b4ff8d29440beec01972d?s=96",! ! ! "description":"",! ! ! "email":"demo@example.com",! ! ! "registered":"2014-06-05T14:39:17+00:00",! ! ! "meta":{! ! ! ! "links":{! ! ! ! ! "self":"http:\/\/wcchi.dev\/wp-json\/users\/1",! ! ! ! ! "archives":"http:\/\/wcchi.dev\/wp-json\/users\/1\/posts"! ! ! ! }! ! ! }! ! },! ! "content":"

The JSON REST API (WP API)< \/a> is coming to WordPress Core in WordPress 4.1. Come to learn more what the JSON REST API is, why this upcoming feature is a game changer<\/strong> for WordPress as an application platform<\/em>, and leave with some examples of how you can be using or extending it today.<\/ p>\n

We will walk through examples of how to make HTTP requests to view resources through the API, and authenticating<\/a> to perform more sensitive actions such as creating, editing, and deleting resources.<\/p>\n",! ! "parent":0,! ! "link":"http:\/\/wcchi.dev\/put-your-content-to-rest-with-wp-api\/",! ! "date":"2014-06-13T11:39:55+05:00",! ! "modified":"2014-06-13T11:39:55+05:00",! ! "format":"standard",! ! "slug":"put-your-content-to-rest-with-wp-api",! ! "guid":"http:\/\/wcchi.dev\/put-your-content-to-rest-with-wp-api\/",! ! "excerpt":"

The JSON REST API (WP API) is coming to WordPress Core in WordPress 4.1. Come to learn more what the JSON REST API is,! ! why this upcoming feature is a game changer for WordPress as an application platform,! ! and leave with some examples of how you can be using or extending it today. We will […]<\/p>\n",! ! "menu_order":0,! ! "comment_status":"open",! ! "ping_status":"open",! ! "sticky":true,! ! "date_tz":"Etc\/GMT-5",! ! "date_gmt":"2014-06-13T16:39:55+00:00",! ! "modified_tz":"Etc\/GMT-5",! ! "modified_gmt":"2014-06-13T16:39:55+00:00",! ! "password":"",! ! "meta":{! ! ! "links":{! ! ! ! "self":"http:\/\/wcchi.dev\/wp-json\/posts\/6",! ! ! ! "author":"http:\/\/wcchi.dev\/wp-json\/users\/1",! ! ! ! "collection":"http:\/\/wcchi.dev\/wp-json\/posts",! ! ! ! "replies":"http:\/\/wcchi.dev\/wp-json\/posts\/6\/comments",! ! ! ! "version-history":"http:\/\/wcchi.dev\/wp-json\/posts\/6\/revisions"! ! ! }!

Slide 34

Slide 34 text

/wp-json/users/1 PUT Request Request curl -X PUT -H "Content-Type: application/json" -d '! {! ! "name":"Rachel Baker",! ! "first_name":"Rachel",! ! "last_name": “Baker",! ! "nickname": “rachelbaker",! ! "email": “rachel\u0040rachelbaker.me"! }! ' -u admin:password http://wcchi.dev/wp-json/users/1/!

Slide 35

Slide 35 text

Response {! ! "ID":1,! ! "username":"admin",! ! "name":"Rachel Baker",! ! "first_name":"Rachel",! ! "last_name":"Baker",! ! "nickname":"rachelbaker",! ! "slug":"admin",! ! "URL":"",! ! "avatar":"http:\/\/0.gravatar.com\/avatar\/634b37a53babc18a5bda19722d5b41a3?s=96",! ! "description":"",! ! "registered":"2014-06-05T14:39:17+00:00",! ! "roles":["administrator"],! ! “capabilities":{! ! ! "switch_themes":true,! ! "edit_themes":true,! ! "activate_plugins":true,! ! "edit_plugins":true,! ! "edit_users":true,! ! "edit_files":true,! ! "manage_options":true,! ! "moderate_comments":true,! ! "manage_categories":true,! ! "manage_links":true,! ! "upload_files":true,! ! "import":true,! ! "unfiltered_html":true,! ! "edit_posts":true,! ! "edit_others_posts":true,! ! "edit_published_posts":true,! ! "publish_posts":true,! ! "edit_pages":true,! ! "read":true,! ! "level_10":true,! ! "level_9":true,! ! "level_8":true,! ! "level_7":true,! ! "level_6":true,! ! "level_5":true,! ! "level_4":true,! ! "level_3":true,! ! "level_2":true,! ! "level_1":true,! ! "level_0":true,! ! "edit_others_pages":true,! ! "edit_published_pages":true,! ! "publish_pages":true,! ! "delete_pages":true,! ! "delete_others_pages":true,! ! "delete_published_pages":true,! ! "delete_posts":true,! ! "delete_others_posts":true,! ! "delete_published_posts":true,! ! "delete_private_posts":true,! ! ! "edit_private_posts":true,!

Slide 36

Slide 36 text

• More about REST: http://www.restapitutorial.com/lessons/ whatisrest.html • Postman HTTP Chrome Plugin: http://www.getpostman.com/ • Paw HTTP App for OSX: http://luckymarmot.com/paw • WP API JS Client: https://github.com/WP-API/client-js • WP API PHP Client: https://github.com/WP-API/client-php • WP API WP-CLI Client: https://github.com/WP-API/client-cli • WP API Console: https://github.com/WP-API/api-console Resources

Slide 37

Slide 37 text

I must go find a quiet place, put on my 
 headphones, and start hacking 
 on something that uses WP API right NOW! Now, I am entranced.

Slide 38

Slide 38 text

Questions? Slides: https://speakerdeck.com/ rachelbaker/put-your-content- to-rest-with-wp-api