WordCamp Europe 2015

WordCamp Europe 2015

Introduction to the REST API

Code available at https://gist.github.com/rmccue/99df1e545bd8bf45c9b8

6ea15b18e25b00e1af953e489439d540?s=128

Ryan McCue

June 27, 2015
Tweet

Transcript

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

    Made
  2. Warning: <code>

  3. What API?

  4. What API? XML-RPC API

  5. What API? Modern REST APIs

  6. What API? Modern REST APIs

  7. Modern REST APIs What API?

  8. Modern REST APIs What API?

  9. What API? WordPress REST API

  10. What API? Lead Developers Ryan McCue Rachel Baker

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

    contributors
  12. What API? Version 2

  13. How do I use it?

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

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

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

    POST Create a new resource PUT Update a resource DELETE Delete a resource
  17. 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
  18. Posts Using the API GET /wp/v2/posts Get the latest posts

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

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

  21. 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"
  22. Using the API Authentication

  23. Authentication Using the API Cookie Authentication

  24. Authentication Using the API OAuth 1.0a

  25. Using the API Try it out!

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

  27. How do I extend it?

  28. Extending Don’t Hack Core

  29. 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; }
  30. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(

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

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

  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 null; } return $posts[0]->post_title; }
  34. 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; }
  35. Extending $ http api.local/wp-json/myplugin/v1/author/999 HTTP/1.1 200 OK Allow: GET null

  36. 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 } }]
  37. Extending Namespaces register_rest_route( 'myplugin/v1', '…', array(…) );

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

    … "namespaces": [ "wp/v2", "myplugin/v1" ] }
  39. 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; }
  40. Extending add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(

    'methods' => 'GET', 'callback' => 'my_awesome_func', ) ); } );
  41. 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', ), ), ) ); } );
  42. 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 ); }, ), ), ) ); } );
  43. 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." } } }]
  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 ); }, ), ), ) ); } );
  45. 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' ); } ) ); } );
  46. 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 } }]
  47. gist.github.com/rmccue
 v2.wp-api.org poststatus.com/wordpress-json-rest-api/

  48. Thanks. @rmccue
 rmccue.io https://gist.github.com/rmccue/99df1e545bd8bf45c9b8