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

Real Time REST API with WordPress

Real Time REST API with WordPress

The release of WordPress 4.7 gave REST API endpoints to 27% of internet. Now the data (posts, comments, taxonomies, terms, users, options, etc.) from within your CMS is available for interaction. In order to get data from your site you have to send a request. The burden is on your API client to pester your API server with requests to ensure they have up to date data.
We live in a crazy mixed-up world with bots, microservices, and IoT. Polling the same API endpoint to see if something changed is taxing and inefficient. There is a better way with Webhooks.
Learn how subscription endpoints are the key to a real-time REST API. Notify a client that a new post published. Post a message to a Slack channel when a new comment is awaiting moderation. Receive a text message when a user resets their password. The use-cases are endless.

Rachel Baker

March 09, 2017

More Decks by Rachel Baker

Other Decks in Programming



    client know if the response data changed?
  2. SOMETIMES YOU NEED A LITTLE PUSH • Updating headless front-end

    application views for content changes (breaking news, new comment added, etc.) • Notifying a client or micro service application of a content event (comment awaiting moderation, post updated, new order received, etc.) • Attempting to keep anything in “sync” between WordPress and another application (users, newsletter subscribers, etc.) • Breaking cache or reseting cache headers
  3. WEBHOOKS A WebHook is an HTTP callback: an HTTP POST

    that occurs when something happens; a simple event-notification via HTTP POST. A web application implementing WebHooks will POST a message to a URL when certain things happen. via https://webhooks.pbworks.com/w/page/13385124/FrontPage
  4. WORDPRESS ACTIONS In WordPress; an Action is a PHP function

    that is executed at specific points throughout the WordPress Core. Developers can create a custom Action using the Action API to add or remove code from an existing Action by specifying any existing Hook. This process is called "hooking". via https://codex.wordpress.org/Glossary#Action
  5. function notify_user_registered( $user_id ) { // authentication handling not shown.

    $request = new WP_REST_Request( 'GET', '/wp/v2/users/' . $user_id ); $response = rest_do_request( $request ); $status = $response->get_status(); // $status should equal 200. $data = $response->get_data(); $callback_uri = 'https://example.com/myclient/callback/ user.registered'; wp_safe_remote_post( $callback_uri, array( 'body' => $data ) ); // check for errors. } add_action( 'user_register', 'notify_user_registered' ); Notify a callback uri when a new user is created
  6. REST HOOKS REST Hooks itself is not a specification, it

    is a collection of patterns that treat webhooks like subscriptions. These subscriptions are manipulated via a REST API just like any other resource. via http://resthooks.org/docs/
  7. WHAT YOU NEED 1. Mechanism to store subscriptions
 2. Mechanism

    to list, create, modify, delete subscriptions via API endpoints
 3. List event hooks via API endpoint
 4. Mechanism to send hooks <—— Database table <—— Subscriptions endpoints and authentication <—— Hooks endpoints <—— Implement needed WordPress actions to send template response data to subscription uris
  8. function notify_user_registered( $user_id ) { // authentication handling not shown.

    $request = new WP_REST_Request( 'GET', '/wp/v2/users/' . $user_id ); $response = rest_do_request( $request ); $status = $response->get_status(); if ( 200 === $status ) { $data = $response->get_data(); $subscriptions = get_available_subscriptions(); foreach( $subscriptions as $subscription ) { if ( empty( $subscription['hook'] ) || 'user.created' !== $subscription['hook'] ) { continue; } wp_safe_remote_post( $subscription['uri'], array( 'body' => $data ) ); // check for errors. } return; } } add_action( 'user_register', 'notify_user_registered' ); Send response data to subscribers when a new user is created
  9. GOTCHAS • Error handling and optionally retrying connections for 4xx/5xx

 • Performance of inline hook delivery. Consider a delayed queue system.
 • Impact of payload size. Consider reducing hook templates where possible.
 • Security and authentication.
 • You now have your own RPC API to maintain alongside your REST API.
  10. ICON CREDITS Pull by Venkatesh A.G.E Prodding by Luis Prado

    Hook by Alex Fuller Cake by Edward Boatman Valentine cake by Dinosoft Labs Newspaper by Noe Araujo Facepalm by Edward Boatman