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

An introduction to the WordPress REST API

C9a5478931da1bbdcc0be2800daa2915?s=47 Jack Lenox
November 19, 2016

An introduction to the WordPress REST API

Presented at WordCamp Athens 2016.

Created using Marp: https://yhatt.github.io/marp/


Jack Lenox

November 19, 2016


  1. An introduc on to the WordPress REST API Jack Lenox

    (@jacklenox) WordCamp Athens 2016
  2. I Greece!

  3. 1. The case for a WordPress REST API

  4. What is a REST API?

  5. It stands for REpresenta onal State Transfer Applica on Programming

  6. Centred around verbs GET POST UPDATE PUT DELETE etc. Different

    ways to interact with your data
  7. Usually responds in XML or JSON format XML (Extensible Markup

    Language) HTML RSS JSON (JavaScript Object Notation)
  8. Why does WordPress need one?

  9. None
  10. MoMA in New York (c. 2009) Their developers wanted to

    use: WordPress as the CMS Ruby to build the front-end
  11. Why? I don't know... but they did

  12. None
  13. WordPress.com API Released in 2012 Available to self-hosted sites via

    Jetpack Powerful and road-tested Not extensible (unless you work at Automa c!)
  14. None
  15. REST API use cases Non-PHP implementations Mobile devices But most

  16. JavaScript OMGBBQ!

  17. None
  18. Internet of things

  19. So, what is the WordPress REST API?

  20. None
  21. None
  22. Merge meline WordPress 4.4 (8 December 2015) Infrastructure merged No

    endpoints WordPress 4.7 (due 6 December 2016) Endpoints merged!
  23. 2. How the WordPress REST API works (aka, what the

    heck is an endpoint?)
  24. End‐what? REST APIs interprets a URL, and return a response

    Not wildly different to RSS Sample request to fetch some posts: wc-athens.dev/wp-json/wp/v2/posts
  25. [ { "id": 1, "date": "2016-11-18T14:08:53", "date_gmt": "2016-11-18T14:08:53", "modified": "2016-11-18T14:08:53",

    "modified_gmt": "2016-11-18T14:08:53", "slug": "hello-world", "type": "post", "link": "http://wp-rest-api-demo.dev/2016/11/18/hello-wor "title": { "rendered": "Hello world!" }, "content": { "rendered": "<p>Welcome to WordPress. This is your fi "protected": false } } ]
  26. More endpoints... Single post: wc-athens.dev/wp-json/wp/v2/posts/1 All pages: wc-athens.dev/wp-json/wp/v2/pages All users:

    wc-athens.dev/wp-json/wp/v2/users All categories: wc-athens.dev/wp-json/wp/v2/categories
  27. Moar verbs! By default, our calls to the REST API

    are GET requests. But we can do a lot more: POST PUT UPDATE DELETE
  28. Authen ca on!

  29. A few ways to authen cate Basic Authen ca on

    Quick and dirty Cookie Authen ca on Good, especially for themes and plugins OAuth 1.0 A bit complicated, but versatile OAuth 2 Less complicated than OAuth 1, but requires HTTPS
  30. Brokered Authen ca on The broker apps.wp-api.org Otherwise, it's OAuth

  31. Modifying endpoints

  32. 3. Uses for the WordPress REST API

  33. Custom wp‐admin interfaces Calypso developer.wordpress.com/calypso/ Happytables www.happytables.com

  34. Mobile applica ons WordPress apps for iOS and Android apps.wordpress.com/mobile/

    Vienna github.com/joehoyle/vienna
  35. Themes Picard github.com/automattic/picard/ Anadama github.com/ryelle/anadama-react/ Foxhound github.com/ryelle/foxhound/

  36. Plugins WooCommerce github.com/woocommerce/woocommerce EDD github.com/easydigitaldownloads/Easy-Digital- Downloads Syndication?

  37. Enterprise Quartz qz.com Facebook facebookbrand.com Google IO (not WordPress, but...)

  38. Very exci ng mes ahead!

  39. Further reading, and any ques ons? The WordPress REST API

    docs v2.wp-api.org WordPress TV wordpress.tv (search "REST API") javascriptforwp.com
  40. Ευχαριστώ πολύ! @jacklenox We're hiring! automa c.com/work‐with‐us/