Slide 1

Slide 1 text

MEET The
 JSON REST API Rachel Baker @rachelbaker 10up.com

Slide 2

Slide 2 text

The What?

Slide 3

Slide 3 text

API

Slide 4

Slide 4 text

REST /posts /posts/4 /posts /posts/4 /posts/4 GET GET POST PUT DELETE Action
 (Verb) Resource
 (Object)

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Use Cases

Slide 7

Slide 7 text

CrUD Operations Among WordPress SITES

Slide 8

Slide 8 text

CrUD Operations WordPress & MOBILE APPS

Slide 9

Slide 9 text

CrUD Operations WordPress & ANYTHING

Slide 10

Slide 10 text

CrUD Operations WITHIN a WORDPRESS SITE

Slide 11

Slide 11 text

GETTING STARTED

Slide 12

Slide 12 text

STEP ONE READ THE DOCUMENTATION

Slide 13

Slide 13 text

STEP TWO Install & Activate the Plugin

Slide 14

Slide 14 text

STEP THREE GET the API ScHEMA http://example.com/wp-json/

Slide 15

Slide 15 text

STEP THREE GET the API ScHEMA http://example.com/wp-json/

Slide 16

Slide 16 text

STEP FOUR GET the POSTS COLLECTION http://example.com/wp-json/posts/

Slide 17

Slide 17 text

http://example.com/wp-json/ STEP FOUR GET the POSTS COLLECTION

Slide 18

Slide 18 text

STEPs FIVE - Twenty-Two EXPLORE ALL THE POSSIBILITES Posts ✔ ✔ ✔ ✔ Pages ✔ ✔ ✔ ✔ Custom Post Types ✔ ✔ ✔ ✔ Post Meta ✔ ✔ ✔ ✔ Media ✔ ✔ ✔ ✔ Comments ✔ ✔ ✔ ✔ Taxonomies/Terms ✔ ✔ ✔ ✔ Users ✔ ✔ ✔ ✔ GET POST PUT DELETE

Slide 19

Slide 19 text

STEP Twenty-THREE - ?? Use the JSON REST API Use the JSON REST API plugin in/for a project. Give us feedback.

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

AUTHENTICATION

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

BASIC AUTH DEVELOPMENT ONLY

Slide 24

Slide 24 text

OAUTH 1.0a SECURE FOR PRODUCTION

Slide 25

Slide 25 text

Custom Auth Write Your own add_filter( 'determine_current_user', 'custom_auth_handler', 20 );

Slide 26

Slide 26 text

THE FUTURE

Slide 27

Slide 27 text

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; } … }

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

HAL EXPLORATION

Slide 32

Slide 32 text

CORE INTEGRATION

Slide 33

Slide 33 text

GET INVOLVED

Slide 34

Slide 34 text

GET INVOLVED

Slide 35

Slide 35 text

JOIN US IN SLACK #core-rest-api add_filter( 'determine_current_user', 'custom_auth_handler', 20 );

Slide 36

Slide 36 text

THANK YOU! github.com/wp-Api/WP-API Questions?? Rachel Baker @rachelbaker 10up.com