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

WordCamp Europe 2015

WordCamp Europe 2015

Introduction to the REST API

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

Ryan McCue

June 27, 2015
Tweet

More Decks by Ryan McCue

Other Decks in Programming

Transcript

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

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

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

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

    Post Types ✔ Post Meta ✔ Revisions ✔ Comments ✔ Terms ✔ Users ✔
  6. 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"
  7. 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; }
  8. 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; }
  9. 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; }
  10. 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 } }]
  11. Extending $ http api.local/wp-json/ HTTP/1.1 200 OK Allow: GET {

    … "namespaces": [ "wp/v2", "myplugin/v1" ] }
  12. 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; }
  13. 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', ), ), ) ); } );
  14. 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 ); }, ), ), ) ); } );
  15. 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." } } }]
  16. 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 ); }, ), ), ) ); } );
  17. 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' ); } ) ); } );
  18. 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 } }]