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

WP REST API

Josh Betz
May 22, 2017
51

WP REST API

Josh Betz

May 22, 2017
Tweet

Transcript

  1. 1. What is REST? 2. Using WP REST API 3.

    Extending WP REST API 4. Examples 5. Discussion
  2. 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
  3. HTTP Status Codes • 1xx (Informational responses) • 2xx (Success)

    • 3xx (Redirection) • 4xx (Client errors) • 5xx (Server errors)
  4. $ 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: <https://demo.wp-api.org/2016/09/03/this-is-a-test-post/>; rel="alternate"; type=text/html
  5. $ 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" }
  6. $ 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 } }
  7. add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\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 ); } ), ), ) ); } );
  8. add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\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 ); } ), ), ) ); } );
  9. add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\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 ); } ), ), ) ); } );
  10. add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\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 ); } ), ), ) ); } );
  11. add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\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 ); } ), ), ) ); } );
  12. add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\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 ); } ), ), ) ); } );
  13. add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\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 ); } ), ), ) ); } );
  14. 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
  15. 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
  16. // 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();
  17. 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; }, ) );
  18. 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; }, ) );
  19. 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; }, ) );
  20. 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; }, ) );
  21. 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; }, ) );
  22. • Liveblog • Syndication • Front-end publishing • React version

    of wp-admin (desktop.wordpress.com) The Future