Meet the JSON REST API

634b37a53babc18a5bda19722d5b41a3?s=47 Rachel Baker
November 08, 2014

Meet the JSON REST API

One of the most exciting changes coming in WordPress is the addition of a JSON REST API. Through the use of the current feature-plugin, developers and users can begin using the API today with backwards compatibility assured in the future. This talk will introduce the JSON REST API, demonstrate how to interact with it, showcase some of the ways the JSON REST API is already being used, and reveal some of the improvements being made in the future.

Given at WordCamp Ventura County. http://2014.ventura.wordcamp.org/sessions/

634b37a53babc18a5bda19722d5b41a3?s=128

Rachel Baker

November 08, 2014
Tweet

Transcript

  1. 3.

    API

  2. 4.

    REST /posts /posts/4 /posts /posts/4 /posts/4 GET GET POST PUT

    DELETE Action
 (Verb) Resource
 (Object)
  3. 5.

    JSON { "bold_folder_labels": true, "caret_style": "phase", "default_line_ending": "unix", "ensure_newline_at_eof_on_save": false,

    "font_face": "Inconsolata LGC", "indent_guide_options": [ "draw_normal", "draw_active" ], "show_line_endings": true, "theme": "Cobalt2.sublime-theme", "trim_automatic_white_space": true, }
  4. 18.

    STEPs FIVE - Twenty-Two EXPLORE ALL THE POSSIBILITES Posts ✔

    ✔ ✔ ✔ Pages ✔ ✔ ✔ ✔ Custom Post Types ✔ ✔ ✔ ✔ Post Meta ✔ ✔ ✔ ✔ Media ✔ ✔ ✔ ✔ Comments ✔ ✔ ✔ ✔ Taxonomies/Terms ✔ ✔ ✔ ✔ Users ✔ ✔ ✔ ✔ GET POST PUT DELETE
  5. 19.

    STEP Twenty-THREE - ?? Use the JSON REST API Use

    the JSON REST API plugin in/for a project. Give us feedback.
  6. 20.

    Backward Compatible and ALREADY IN USE Wired New York Times

    WordCamp Central http://2014.ventura.wordcamp.org/ Reactor http://reactor.apppresser.com/ 10up http://10up.com Rachel Baker (me) http://rachelbaker.me
  7. 22.

    COOKIE Auth Nonces INCLUDED function json_register_scripts() { wp_register_script( 'wp-api', 'http://wp-api.github.io/client-js/

    build/js/wp-api.js', array( 'jquery', 'backbone', 'underscore' ), '1.1', true ); $settings = array( 'root' => esc_url_raw( get_json_url() ), 'nonce' => wp_create_nonce( 'wp_json' ) ); wp_localize_script( 'wp-api', 'WP_API_Settings', $settings ); } add_action( 'wp_enqueue_scripts', 'json_register_scripts', -100 ); add_action( 'admin_enqueue_scripts', 'json_register_scripts', -100 );
  8. 27.

    Improved Extensibility /** * Access the collection and resources for

    your custom route */ class Your_Custom_Controller extends WP_JSON_Controller { public function get_items( WP_JSON_Request $request ) { $items = get_custom_items( $request['parameter'], $args ); if ( is_wp_error( $items ) ) { return new WP_Error( 'json_invalid_custom_request', __( "Items don't exist" ), array( 'status' => 404 ) ); } foreach ( $items as &$item ) { $item = self::prepare_item_for_response( $item, $request ); } return $items; } … }
  9. 28.

    Improved Extensibility /** * Access the collection and resources for

    your custom route */ class Your_Custom_Controller extends WP_JSON_Controller { … public function get_item( $request ) { // your custom get item logic here. return self::prepare_item_for_response( $item ); } public function update_item( $request ) { // your custom update logic here. return self::prepare_item_for_response( $item ); } … public function prepare_item_for_response( $item ) { // create your own custom response. return array( 'id' => (int) $item->id, 'slug' => $item->slug, ); } }
  10. 29.

    NAMESPACED ROUTES function register_json_route( $namespace, $route, $args = array(), $override

    = false ) { global $wp_json_server; if ( isset( $args['callback'] ) ) { // Upgrade a single set to multiple $args = array( $args ); } $defaults = array( 'methods' => 'GET', 'callback' => null, 'args' => array(), ); foreach ( $args as &$arg_group ) { $arg_group = array_merge( $defaults, $arg_group ); } $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' ); $wp_json_server->register_route( $full_route, $args, $override ); }
  11. 30.

    IMPROVED ROUTE Registration register_json_route( 'prefix', '/resources', array( 'methods' => 'GET',

    'callback' => array( $controller, 'get_items' ), 'args' => array( 'foo' => array( 'required' => true, ), 'bar' => array( 'required' => false, ), ), ) );