Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Put Your Content to REST with WP API

Put Your Content to REST with WP API

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 walk through examples of how to make HTTP requests to view resources through the API, and authenticating to perform more sensitive actions such as creating, editing, and deleting resources.

Rachel Baker

June 13, 2014
Tweet

More Decks by Rachel Baker

Other Decks in Programming

Transcript

  1. •Posts •Pages •Custom Post Types •Post Meta •Media •Comments •Taxonomies

    •Users •….or extend as needed WP API: Create, Read, Update, and Delete
  2. 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.
  3. 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.
  4. 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)
  5. 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.

  6. • 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
  7. • 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
  8. •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
  9. •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
  10. •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
  11. • 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
  12. • 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
  13. • 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
  14. • 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
  15. • 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
  16. /wp-json/posts POST curl -X POST -H "Content-Type: application/json" -d '!

    {! ! "title":"Put Your Content to REST with WP API",! ! "content_raw":"<p>The <a href=\"https:\/\/github.com\/WP-API\/WP-API\" title=\"WordPress JSON REST API\" target=\"_blank\">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 <strong>game changer<\/strong> for WordPress as an <em>application platform<\/em>, and leave with some examples of how you can be using or extending it today.<\/p><p>We will walk through examples of how to make HTTP requests to view resources through the API, and <a href= \"https:\/\/github.com\/WP-API\/WP-API\/blob\/master\/docs\/authentication.md\" target=\"_blank\">authenticating<\/a> to perform more sensitive actions such as creating, editing, and deleting resources.<\/p>",! ! "status": "publish",! ! "sticky": true,! ! "post_meta" : [! ! ! {! ! ! ! "key" : "speaker_name",! ! ! ! "value" : "Rachel Baker"! ! ! },! ! ! {! ! ! ! "key" : "speaker_twitter",! ! ! ! "value" : "@rachelbaker"! ! ! }! ! ]! }! ' -u admin:password http://wcchi.dev/wp-json/posts/! Request Basic Auth
  17. 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":"[email protected]",! ! ! "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":"<p>The <a href=\"https:\/\/github.com\/WP-API\/WP-API\" title=\"WordPress JSON REST API\" target=\"_blank\">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 <strong>game changer<\/strong> for WordPress as an <em>application platform<\/em>, and leave with some examples of how you can be using or extending it today.<\/ p>\n<p>We will walk through examples of how to make HTTP requests to view resources through the API, and <a href=\"https:\/\/github.com\/WP-API\/WP- API\/blob\/master\/docs\/authentication.md\" target=\"_blank\">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":"<p>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 [&hellip;]<\/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"! ! ! }!
  18. /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/!
  19. 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,!
  20. • 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
  21. 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.