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

An introduction to the WordPress REST API

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
Tweet

More Decks by Jack Lenox

Other Decks in Technology

Transcript

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

    (@jacklenox) WordCamp Athens 2016
  2. Usually responds in XML or JSON format XML (Extensible Markup

    Language) HTML RSS JSON (JavaScript Object Notation)
  3. MoMA in New York (c. 2009) Their developers wanted to

    use: WordPress as the CMS Ruby to build the front-end
  4. 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!)
  5. Merge meline WordPress 4.4 (8 December 2015) Infrastructure merged No

    endpoints WordPress 4.7 (due 6 December 2016) Endpoints merged!
  6. 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
  7. [ { "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 } } ]
  8. 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
  9. Moar verbs! By default, our calls to the REST API

    are GET requests. But we can do a lot more: POST PUT UPDATE DELETE
  10. 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
  11. Further reading, and any ques ons? The WordPress REST API

    docs v2.wp-api.org WordPress TV wordpress.tv (search "REST API") javascriptforwp.com