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

WordCamp Brisbane 2015

WordCamp Brisbane 2015

Introduction to using and extending the WordPress REST API

Ryan McCue

May 31, 2015
Tweet

More Decks by Ryan McCue

Other Decks in Programming

Transcript

  1. The WordPress REST API
    Ryan McCue

    Senior Engineer at Human Made

    View full-size slide

  2. What API?
    XML-RPC API

    View full-size slide

  3. What API?
    Modern REST APIs

    View full-size slide

  4. What API?
    Modern REST APIs

    View full-size slide

  5. Modern REST APIs
    What API?

    View full-size slide

  6. Modern REST APIs
    What API?

    View full-size slide

  7. What API?
    WordPress REST API

    View full-size slide

  8. What API?
    Lead Developers
    Ryan McCue Rachel Baker

    View full-size slide

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

    View full-size slide

  10. What API?
    Version 2

    View full-size slide

  11. How do I use it?

    View full-size slide

  12. Using the API
    Install the Plugin
    Version 1: Plugins > Beta Testing > JSON REST API
    Version 2: github.com/WP-API/WP-API

    View full-size slide

  13. Using the API
    What is REST?

    View full-size slide

  14. What is REST?
    Using the API
    GET Get the resource
    POST Create a new resource
    PUT Update a resource
    DELETE Delete a resource

    View full-size slide

  15. 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

    View full-size slide

  16. Posts
    Using the API
    GET /wp/v2/posts Get the latest posts
    POST /wp/v2/posts Create a new post

    View full-size slide

  17. Using the API
    Posts ✔
    Pages ✔
    Media ✔
    Custom Post Types ✔
    Post Meta ✔
    Revisions ✔
    Comments ✔
    Terms ✔
    Users ✔

    View full-size slide

  18. Using the API
    Discovery

    View full-size slide

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

    <== Link: ;

    rel="http://github.com/WP-API/WP-API"

    View full-size slide

  20. Using the API
    Authentication

    View full-size slide

  21. Authentication
    Using the API
    Cookie Authentication

    View full-size slide

  22. Authentication
    Using the API
    OAuth 1.0a

    View full-size slide

  23. Using the API
    Try it out!

    View full-size slide

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

    View full-size slide

  25. How do I extend it?

    View full-size slide

  26. Extending
    Don’t Hack Core

    View full-size slide

  27. 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;
    }

    View full-size slide

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

    View full-size slide

  29. Extending
    $ http api.local/wp-json/myplugin/v1/author/1
    HTTP/1.1 200 OK
    Allow: GET
    "Hello!"

    View full-size slide

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

    View full-size slide

  31. 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;
    }

    View full-size slide

  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 new WP_Error( 'no_author', 'Invalid author',

    array( 'status' => 404 ) );
    }
    return $posts[0]->post_title;
    }

    View full-size slide

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

    View full-size slide

  34. 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 }
    }]

    View full-size slide

  35. Extending
    Namespaces
    register_rest_route( 'myplugin/v1', '…', array(…) );

    View full-size slide

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

    "namespaces": [
    "wp/v2",
    "myplugin/v1"
    ]
    }

    View full-size slide

  37. 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;
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. 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."
    }
    }
    }]

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  44. 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
    }
    }]

    View full-size slide

  45. gist.github.com/rmccue

    v2.wp-api.org
    poststatus.com/wordpress-json-rest-api/

    View full-size slide

  46. Thanks.
    @rmccue

    rmccue.io

    View full-size slide