$30 off During Our Annual Pro Sale. View Details »

WP REST API

Josh Betz
May 22, 2017
38

WP REST API

Josh Betz

May 22, 2017
Tweet

Transcript

  1. WP REST API
    Josh Betz
    https://josh.blog

    View Slide

  2. 1. What is REST?
    2. Using WP REST API
    3. Extending WP REST API
    4. Examples
    5. Discussion

    View Slide

  3. CHAPTER I
    What is REST?

    View Slide

  4. Representational state transfer (REST) or RESTful Web
    services are one way of providing interoperability between
    computer systems on the Internet. REST-compliant Web
    services allow requesting systems to access and manipulate
    textual representations of Web resources using a uniform
    and predefined set of stateless operations.
    https://en.wikipedia.org/wiki/Representational_state_transfer

    View Slide

  5. Terms
    • Route/Path
    • Method
    • Endpoint
    • Header
    • Response Code (Status)

    View Slide

  6. /wp-json/wp/v2/posts

    View Slide

  7. • GET
    • POST
    • PUT
    • PATCH
    • DELETE
    HTTP Methods

    View Slide

  8. GET /wp-json/wp/v2/posts

    View Slide

  9. DELETE /wp-json/wp/v2/posts/470

    View Slide

  10. HTTP Status Codes
    • 1xx (Informational responses)
    • 2xx (Success)
    • 3xx (Redirection)
    • 4xx (Client errors)
    • 5xx (Server errors)

    View Slide

  11. $ curl -I http://demo.wp-api.org/wp-json/wp/v2/posts/470
    HTTP/1.1 200 OK
    Content-Type: application/json; charset=UTF-8
    Connection: keep-alive
    Access-Control-Allow-Headers: Authorization, Content-Type
    Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
    Allow: GET
    Cache-Control: no-cache, must-revalidate, max-age=0
    Date: Wed, 26 Apr 2017 17:21:09 GMT
    Link: ;
    rel="alternate"; type=text/html

    View Slide

  12. CHAPTER II
    Using WP REST API

    View Slide

  13. $ curl http://demo.wp-api.org/wp-json/wp/v2/posts/470 | jq .content
    {
    "rendered": "This is a test post!\n",
    "protected": false
    }

    View Slide

  14. $ curl http://demo.wp-api.org/wp-json | jq .namespaces
    [
    "oembed/1.0",
    "broker/v1",
    "liveblog-likes/v1",
    "wp/v2"
    ]

    View Slide

  15. $ curl http://demo.wp-api.org/wp-json | jq .authentication
    {
    "oauth1": {
    "request": "https://demo.wp-api.org/oauth1/request",
    "authorize": "https://demo.wp-api.org/oauth1/authorize",
    "access": "https://demo.wp-api.org/oauth1/access",
    "version": "0.1"
    },
    "broker": "https://demo.wp-api.org/wp-json/broker/v1/connect"
    }

    View Slide

  16. $ curl -X POST http://demo.wp-api.org/wp-json/wp/v2/posts -d
    "post_title=test" | jq .
    {
    "code": "rest_cannot_create",
    "message": "Sorry, you are not allowed to create posts as this
    user.",
    "data": {
    "status": 401
    }
    }

    View Slide

  17. CHAPTER III
    Extending WP REST API

    View Slide

  18. add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
    'methods' => WP_REST_Server::READABLE, // GET
    'permission_callback' => function() => {
    return current_user_can( 'edit_others_posts' );
    },
    'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },
    'args' => array(
    'id' => array(
    'validate_callback' => function( $param, $request, $key ) {
    return is_numeric( $param );
    }
    ),
    ),
    ) );
    } );

    View Slide

  19. add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
    'methods' => WP_REST_Server::READABLE, // GET
    'permission_callback' => function() => {
    return current_user_can( 'edit_others_posts' );
    },
    'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },
    'args' => array(
    'id' => array(
    'validate_callback' => function( $param, $request, $key ) {
    return is_numeric( $param );
    }
    ),
    ),
    ) );
    } );

    View Slide

  20. add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
    'methods' => WP_REST_Server::READABLE, // GET
    'permission_callback' => function() => {
    return current_user_can( 'edit_others_posts' );
    },
    'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },
    'args' => array(
    'id' => array(
    'validate_callback' => function( $param, $request, $key ) {
    return is_numeric( $param );
    }
    ),
    ),
    ) );
    } );

    View Slide

  21. add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
    'methods' => WP_REST_Server::READABLE, // GET
    'permission_callback' => function() => {
    return current_user_can( 'edit_others_posts' );
    },
    'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },
    'args' => array(
    'id' => array(
    'validate_callback' => function( $param, $request, $key ) {
    return is_numeric( $param );
    }
    ),
    ),
    ) );
    } );

    View Slide

  22. add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
    'methods' => WP_REST_Server::READABLE, // GET
    'permission_callback' => function() => {
    return current_user_can( 'edit_others_posts' );
    },
    'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },
    'args' => array(
    'id' => array(
    'validate_callback' => function( $param, $request, $key ) {
    return is_numeric( $param );
    }
    ),
    ),
    ) );
    } );

    View Slide

  23. add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
    'methods' => WP_REST_Server::READABLE, // GET
    'permission_callback' => function() => {
    return current_user_can( 'edit_others_posts' );
    },
    'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },
    'args' => array(
    'id' => array(
    'validate_callback' => function( $param, $request, $key ) {
    return is_numeric( $param );
    }
    ),
    ),
    ) );
    } );

    View Slide

  24. add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
    'methods' => WP_REST_Server::READABLE, // GET
    'permission_callback' => function() => {
    return current_user_can( 'edit_others_posts' );
    },
    'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },
    'args' => array(
    'id' => array(
    'validate_callback' => function( $param, $request, $key ) {
    return is_numeric( $param );
    }
    ),
    ),
    ) );
    } );

    View Slide

  25. After your callback is called, the return value is then
    converted to JSON, and returned to the client. This allows
    you to return basically any form of data.
    https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#return-value

    View Slide

  26. 'callback' => function( WP_REST_Request $request ) => {
    return array( 'success' => true );
    },

    View Slide

  27. By default, routes receive all arguments passed in from the
    request… as the first parameter to your endpoint.
    https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#arguments

    View Slide

  28. // You can access parameters via direct array access on the object:
    $param = $request['some_param'];
    // Or via the helper method:
    $param = $request->get_param( 'some_param' );
    // You can get the combined, merged set of parameters:
    $parameters = $request->get_params();
    // The individual sets of parameters are also available, if needed:
    $parameters = $request->get_url_params();
    $parameters = $request->get_query_params();
    $parameters = $request->get_body_params();
    $parameters = $request->get_json_params();
    $parameters = $request->get_default_params();
    // Uploads aren't merged in, but can be accessed separately:
    $parameters = $request->get_file_params();

    View Slide

  29. register_rest_field( 'comment', 'karma', array(
    'get_callback' => function( $comment_arr ) {
    $comment_obj = get_comment( $comment_arr['id'] );
    return (int) $comment_obj->comment_karma;
    },
    'update_callback' => function( $karma, $comment_obj ) {
    $ret = wp_update_comment( array(
    'comment_ID' => $comment_obj->comment_ID,
    'comment_karma' => $karma
    ) );
    if ( false === $ret ) {
    return new WP_Error( 'rest_comment_karma_failed',
    __( 'Failed to update comment karma.' ), array( 'status' => 500 ) );
    }
    return true;
    },
    ) );

    View Slide

  30. register_rest_field( 'comment', 'karma', array(
    'get_callback' => function( $comment_arr ) {
    $comment_obj = get_comment( $comment_arr['id'] );
    return (int) $comment_obj->comment_karma;
    },
    'update_callback' => function( $karma, $comment_obj ) {
    $ret = wp_update_comment( array(
    'comment_ID' => $comment_obj->comment_ID,
    'comment_karma' => $karma
    ) );
    if ( false === $ret ) {
    return new WP_Error( 'rest_comment_karma_failed',
    __( 'Failed to update comment karma.' ), array( 'status' => 500 ) );
    }
    return true;
    },
    ) );

    View Slide

  31. register_rest_field( 'comment', 'karma', array(
    'get_callback' => function( $comment_arr ) {
    $comment_obj = get_comment( $comment_arr['id'] );
    return (int) $comment_obj->comment_karma;
    },
    'update_callback' => function( $karma, $comment_obj ) {
    $ret = wp_update_comment( array(
    'comment_ID' => $comment_obj->comment_ID,
    'comment_karma' => $karma
    ) );
    if ( false === $ret ) {
    return new WP_Error( 'rest_comment_karma_failed',
    __( 'Failed to update comment karma.' ), array( 'status' => 500 ) );
    }
    return true;
    },
    ) );

    View Slide

  32. register_rest_field( 'comment', 'karma', array(
    'get_callback' => function( $comment_arr ) {
    $comment_obj = get_comment( $comment_arr['id'] );
    return (int) $comment_obj->comment_karma;
    },
    'update_callback' => function( $karma, $comment_obj ) {
    $ret = wp_update_comment( array(
    'comment_ID' => $comment_obj->comment_ID,
    'comment_karma' => $karma
    ) );
    if ( false === $ret ) {
    return new WP_Error( 'rest_comment_karma_failed',
    __( 'Failed to update comment karma.' ), array( 'status' => 500 ) );
    }
    return true;
    },
    ) );

    View Slide

  33. register_rest_field( 'comment', 'karma', array(
    'get_callback' => function( $comment_arr ) {
    $comment_obj = get_comment( $comment_arr['id'] );
    return (int) $comment_obj->comment_karma;
    },
    'update_callback' => function( $karma, $comment_obj ) {
    $ret = wp_update_comment( array(
    'comment_ID' => $comment_obj->comment_ID,
    'comment_karma' => $karma
    ) );
    if ( false === $ret ) {
    return new WP_Error( 'rest_comment_karma_failed',
    __( 'Failed to update comment karma.' ), array( 'status' => 500 ) );
    }
    return true;
    },
    ) );

    View Slide

  34. https://developer.wordpress.org/rest-api/

    View Slide

  35. CHAPTER IV
    Examples

    View Slide

  36. View Slide

  37. View Slide

  38. • Liveblog
    • Syndication
    • Front-end publishing
    • React version of wp-admin
    (desktop.wordpress.com)
    The Future

    View Slide

  39. Thanks
    Josh Betz
    https://josh.blog

    View Slide