WordCamp Brisbane 2015

WordCamp Brisbane 2015

Introduction to using and extending the WordPress REST API

6ea15b18e25b00e1af953e489439d540?s=128

Ryan McCue

May 31, 2015
Tweet

Transcript

  1. The WordPress REST API Ryan McCue
 Senior Engineer at Human

    Made
  2. What API?

  3. What API? XML-RPC API

  4. What API? Modern REST APIs

  5. What API? Modern REST APIs

  6. Modern REST APIs What API?

  7. Modern REST APIs What API?

  8. What API? WordPress REST API

  9. What API? Lead Developers Ryan McCue Rachel Baker

  10. What API? Contributing Developers Daniel Bachhuber Joe Hoyle 53 total

    contributors
  11. What API? Version 2

  12. How do I use it?

  13. Using the API Install the Plugin Version 1: Plugins >

    Beta Testing > JSON REST API Version 2: github.com/WP-API/WP-API
  14. Using the API What is REST?

  15. What is REST? Using the API GET Get the resource

    POST Create a new resource PUT Update a resource DELETE Delete a resource
  16. Single Post Using the API GET /wp/v2/posts/42 Get post 42

    PUT /wp/v2/posts/42 Update post 42 DELETE /wp/v2/posts/42 Delete (trash) post 42
  17. Posts Using the API GET /wp/v2/posts Get the latest posts

    POST /wp/v2/posts Create a new post
  18. Using the API Posts ✔ Pages ✔ Media ✔ Custom

    Post Types ✔ Post Meta ✔ Revisions ✔ Comments ✔ Terms ✔ Users ✔
  19. Using the API Discovery

  20. Using the API Discovery ==> HEAD http://www.wired.com/ <== HTTP/1.1 200

    OK
 <== Link: <http://www.wired.com/wp-json/>;
 rel="http://github.com/WP-API/WP-API"
  21. Using the API Authentication

  22. Authentication Using the API Cookie Authentication

  23. Authentication Using the API OAuth 1.0a

  24. Using the API Try it out!

  25. Try it out! Using the API brisbane.wordcamp.org/2015/wp-json/ www.wired.com/wp-json/ poststatus.com/wp-json/

  26. How do I extend it?

  27. Extending Don’t Hack Core

  28. Extending /** * Grab latest post title by an author!

    * * @param array $data Options for the function. * @return string|null Post title for the latest,
 * or null if none. */ function my_awesome_func( $data ) { $posts = get_posts( array( 'author' => $data['id'], ) ); if ( empty( $posts ) ) { return null; } return $posts[0]->post_title; }
  29. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(

    'methods' => 'GET', 'callback' => 'my_awesome_func', ) ); } );
  30. Extending $ http api.local/wp-json/myplugin/v1/author/1 HTTP/1.1 200 OK Allow: GET "Hello!"

  31. Extending $ http api.local/wp-json/myplugin/v1/author/999 HTTP/1.1 200 OK Allow: GET null

  32. Extending /** * Grab latest post title by an author!

    * * @param array $data Options for the function. * @return string|null Post title for the latest,
 * or null if none. */ function my_awesome_func( $data ) { $posts = get_posts( array( 'author' => $data['id'], ) ); if ( empty( $posts ) ) { return null; } return $posts[0]->post_title; }
  33. Extending /** * Grab latest post title by an author!

    * * @param array $data Options for the function. * @return string|null Post title for the latest,
 * or null if none. */ function my_awesome_func( $data ) { $posts = get_posts( array( 'author' => $data['id'], ) ); if ( empty( $posts ) ) { return new WP_Error( 'no_author', 'Invalid author',
 array( 'status' => 404 ) ); } return $posts[0]->post_title; }
  34. Extending $ http api.local/wp-json/myplugin/v1/author/999 HTTP/1.1 200 OK Allow: GET null

  35. Extending $ http api.local/wp-json/myplugin/v1/author/999 HTTP/1.1 404 Not Found Allow: GET

    [{ "code": "no_author", "message": "Invalid author", "data": { "status": 404 } }]
  36. Extending Namespaces register_rest_route( 'myplugin/v1', '…', array(…) );

  37. Extending $ http api.local/wp-json/ HTTP/1.1 200 OK Allow: GET {

    … "namespaces": [ "wp/v2", "myplugin/v1" ] }
  38. Extending /** * Grab latest post title by an author!

    * * @param array $data Options for the function. * @return string|null Post title for the latest,
 * or null if none. */ function my_awesome_func( $data ) { $posts = get_posts( array( 'author' => $data['id'], ) ); if ( empty( $posts ) ) { return new WP_Error( 'no_author', 'Invalid author',
 array( 'status' => 404 ) ); } return $posts[0]->post_title; }
  39. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(

    'methods' => 'GET', 'callback' => 'my_awesome_func', ) ); } );
  40. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(

    'methods' => 'GET', 'callback' => 'my_awesome_func', 'args' => array( 'id' => array( 'sanitize_callback' => 'absint', ), ), ) ); } );
  41. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>.+)', array(

    'methods' => 'GET', 'callback' => 'my_awesome_func', 'args' => array( 'id' => array( 'validate_callback' => function ( $val ) { return is_numeric( $val ); }, ), ), ) ); } );
  42. Extending $ http api.local/wp-json/myplugin/v1/author/foo HTTP/1.1 400 Bad Request Allow: GET

    [{ "code": "rest_invalid_param", "message": "Invalid parameter(s): Invalid param.", "data": { "status": 400, "params": { "id": "Invalid param." } } }]
  43. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>.+)', array(

    'methods' => 'GET', 'callback' => 'my_awesome_func', 'args' => array( 'id' => array( 'validate_callback' => function ( $val ) { return is_numeric( $val ); }, ), ), ) ); } );
  44. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>.+)', array(

    'methods' => 'GET', 'callback' => 'my_awesome_func', 'args' => array( 'id' => array( 'validate_callback' => function ( $val ) { return is_numeric( $val ); }, ), ), 'permission_callback' => function () { return current_user_can( 'manage_options' ); } ) ); } );
  45. Extending $ http api.local/wp-json/myplugin/v1/author/4 HTTP/1.1 403 Forbidden Allow: GET [{

    "code": "rest_forbidden", "message": "You don't have permission to do this.", "data": { "status": 403 } }]
  46. gist.github.com/rmccue
 v2.wp-api.org poststatus.com/wordpress-json-rest-api/

  47. Thanks. @rmccue
 rmccue.io