Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

CHAPTER I What is REST?

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

/wp-json/wp/v2/posts

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

GET /wp-json/wp/v2/posts

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

$ 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

Slide 12

Slide 12 text

CHAPTER II Using WP REST API

Slide 13

Slide 13 text

$ curl http://demo.wp-api.org/wp-json/wp/v2/posts/470 | jq .content { "rendered": "

This is a test post!

\n", "protected": false }

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

$ 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" }

Slide 16

Slide 16 text

$ 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 } }

Slide 17

Slide 17 text

CHAPTER III Extending WP REST API

Slide 18

Slide 18 text

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 ); } ), ), ) ); } );

Slide 19

Slide 19 text

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 ); } ), ), ) ); } );

Slide 20

Slide 20 text

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 ); } ), ), ) ); } );

Slide 21

Slide 21 text

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 ); } ), ), ) ); } );

Slide 22

Slide 22 text

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 ); } ), ), ) ); } );

Slide 23

Slide 23 text

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 ); } ), ), ) ); } );

Slide 24

Slide 24 text

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 ); } ), ), ) ); } );

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

// 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();

Slide 29

Slide 29 text

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; }, ) );

Slide 30

Slide 30 text

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; }, ) );

Slide 31

Slide 31 text

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; }, ) );

Slide 32

Slide 32 text

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; }, ) );

Slide 33

Slide 33 text

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; }, ) );

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

CHAPTER IV Examples

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Thanks Josh Betz https://josh.blog