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. Put Your Content to
    REST with WP API
    Rachel Baker @rachelbaker WC Chicago 2014

    View Slide

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

    View Slide

  3. Ryan McCue and Contributors

    View Slide

  4. Coming to WordPress 4.1

    View Slide

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

    View Slide

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

    View Slide

  7. Meh.

    View Slide

  8. 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.

    View Slide

  9. 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.

    View Slide

  10. 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)

    View Slide

  11. 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.


    View Slide

  12. Doesn’t
    WordPress
    already have
    API?

    View Slide

  13. • 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

    View Slide

  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
    WordPress.com JSON REST API

    View Slide

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

    View Slide

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

    View Slide

  17. •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

    View Slide

  18. •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

    View Slide

  19. •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

    View Slide

  20. Interesting…
    How would I go about using 

    WP API?

    View Slide

  21. • 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

    View Slide

  22. • 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

    View Slide

  23. /wp-json/
    GET

    View Slide

  24. /wp-json/posts
    GET

    View Slide

  25. /wp-json/taxonomies
    GET

    View Slide

  26. /wp-json/media
    GET

    View Slide

  27. /wp-json/posts/1/comments
    GET

    View Slide

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

    View Slide

  29. • 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

    View Slide

  30. • 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

    View Slide

  31. • 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

    View Slide

  32. /wp-json/posts
    POST
    curl -X POST -H "Content-Type: application/json" -d '!
    {!
    ! "title":"Put Your Content to REST with WP API",!
    ! "content_raw":"The title=\"WordPress JSON REST API\" target=\"_blank\">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 \"https:\/\/github.com\/WP-API\/WP-API\/blob\/master\/docs\/authentication.md\"
    target=\"_blank\">authenticating to perform more sensitive actions such as
    creating, editing, and deleting resources.",!
    ! "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

    View Slide

  33. 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":"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 for WordPress as an application platform, and leave with some examples of how you can be using or extending it today.p>\nWe will walk through examples of how to make HTTP requests to view resources through the API, and API\/blob\/master\/docs\/authentication.md\" target=\"_blank\">authenticating to perform more sensitive actions such as creating, editing, and
    deleting resources.\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 […]\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"!
    ! ! }!

    View Slide

  34. /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/!

    View Slide

  35. 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,!

    View Slide

  36. • 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

    View Slide

  37. 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.

    View Slide

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

    View Slide