Slide 1

Slide 1 text

Rachel Baker Lead Developer : WP REST API Project Lead Engineer: The Wirecutter Utilizing the

Slide 2

Slide 2 text

Getting Started

Slide 3

Slide 3 text

Install the Plugin $ git clone https://github.com/WP-API/WP-API.git wp-rest-api Or download the latest v2.0 Beta release Clone the develop branch from Github into your plugins directory https://github.com/WP-API/WP-API/releases

Slide 4

Slide 4 text

Available Endpoints

Slide 5

Slide 5 text

Embed Related Data

Slide 6

Slide 6 text

Embed the _links https://example.com/wp-json/wp/v2/posts/1?_embed "_links": { … "author": [ { "embeddable": true, "href": "http://local.wpapi.dev/wp-json/wp/v2/users/2" } ], … Hypermedia Application Language HAL

Slide 7

Slide 7 text

Display Responses

Slide 8

Slide 8 text

Posts Collection

Slide 9

Slide 9 text

Using jQuery ( function( $, _ ) { var apiUrl = $( 'link[rel="https://github.com/WP-API/WP-API"]' ).attr( 'href' ), $el = $( '#js-data' ), tmpl = '

<%= title %>

<%= content %>'; $.get( apiUrl + '/wp/v2/posts', function( data ) { for ( var key in data ) { var output = { id : data[ key ].id, title : data[ key ].title.rendered, content : data[ key ].content.rendered }, $template = $( _.template( tmpl, output ) ); $el.append( $template ); } }); })( jQuery, _ ); and Underscore

Slide 10

Slide 10 text

Using PHP $fetch = wp_remote_get( home_url( '/wp-json/wp/v2/posts' ), array() ); // Add error checking. $response_data = wp_remote_retrieve_body( $fetch ); // Add error checking. $items = json_decode( $response_data ); $output = ''; foreach ( $items as $data ) { $output .= sprintf( '

%s

%s', $data->id, $data->title->rendered, $data->content->rendered ); } echo $output;

Slide 11

Slide 11 text

Custom Post Types

Slide 12

Slide 12 text

Include a Post Type

Slide 13

Slide 13 text

Add Events Endpoints Using the init action function wpsd_add_tribe_events_args() { global $wp_post_types; $wp_post_types['tribe_events']->show_in_rest = true; $wp_post_types['tribe_events']->rest_base = 'tribe_events'; $wp_post_types['tribe_events']->rest_controller_class = 'WP_REST_Posts_Controller'; } add_action( 'init', 'wpsd_add_tribe_events_args', 30 );

Slide 14

Slide 14 text

Add Events Endpoints function wpsd_add_tribe_events_args( $args ) { $args['show_in_rest'] = true; $args['rest_base'] = 'tribe_events'; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; return $args; } add_filter( 'tribe_events_register_event_type_args', 'wpsd_add_tribe_events_args' ); Using the tribe_events_register_event_type_args filter

Slide 15

Slide 15 text

Modifying Endpoints

Slide 16

Slide 16 text

Add an Endpoint Field

Slide 17

Slide 17 text

Add Event URL Field function wpsd_register_event_url() { $schema = array( 'type' => 'uri', 'description' => 'URL of the event', 'context' => array( 'view', 'edit' ), ); register_api_field( 'tribe_events', 'event_url', array( 'schema' => $schema, 'get_callback' => 'wpsd_event_url_get_callback', 'update_callback' => 'wpsd_event_url_update_callback', ) ); } add_action( 'rest_api_init', 'wpsd_register_event_url' );

Slide 18

Slide 18 text

Event URL Field Callbacks function wpsd_event_url_get_callback( $post_data ) { $event_url = get_post_meta( $post_data['id'], '_EventURL', true ); return esc_url( $event_url ); } function wpsd_event_url_update_callback( $value, $post ) { if ( ! is_string( $value ) ) { return new WP_Error( 'rest_meta_event_url_invalid', __( 'The event_url value is expected to be a string.' ), array( 'status' => 403 ) ); } $value = esc_url_raw( $value ); $update = update_post_meta( $post->ID, '_EventURL', $value ); // Add error checking. return $update; }

Slide 19

Slide 19 text

Filter Response Data

Slide 20

Slide 20 text

Remove Ping Status function wpsd_remove_ping_status( $response ) { if ( ! current_user_can( 'edit_posts' ) ) { unset( $response->data['ping_status'] ); } return $response; } add_filter( 'rest_prepare_post', 'wpsd_remove_ping_status' );

Slide 21

Slide 21 text

Custom Endpoints

Slide 22

Slide 22 text

Register a Route

Slide 23

Slide 23 text

Register Options Routes function wpsd_add_options_routes() { $collection_args = array( 'methods' => WP_REST_Server::READABLE, 'callback' => 'wpsd_get_options', 'permission_callback' => 'wpsd_options_permission_check', ); $single_args = array( 'methods' => WP_REST_Server::READABLE, 'callback' => 'wpsd_get_option', 'permission_callback' => 'wpsd_options_permission_check', ); register_rest_route( 'wps_demo/v1', '/options', $collection_args ); register_rest_route( 'wps_demo/v1', '/options/(?P[\w-]+)', $single_args ); } add_action( 'rest_api_init', 'wpsd_add_options_routes' ); Just an example of how to create your own route. Do NOT re-use this code.

Slide 24

Slide 24 text

Check Route Permissions function wpsd_options_permission_check( $request ) { if ( current_user_can( 'manage_options' ) ) { return true; } return new WP_Error( 'wpsd_options_access_denied', __( 'Shame on you! You should not be here.' ), array( 'status' => 403 ) ); }

Slide 25

Slide 25 text

Handle Collection Request function wpsd_get_options( $request ) { $all_options = wp_load_alloptions(); $response = rest_ensure_response( $all_options ); $response->add_link( 'self', rest_url( '/wps_demo/v1/options' ) ); return $response; } Loading all the options like this is BAD! This is only an example.

Slide 26

Slide 26 text

Handle Item Request function wpsd_get_option( $request ) { $key = sanitize_key( $request['key'] ); $option = array(); $option[ $key ] = get_option( $key ); // Add error handling. $response = rest_ensure_response( $option ); $response->add_link( 'self', rest_url( '/wps_demo/v1/options/' . $key ) ); $response->add_link( 'collection', rest_url( '/wps_demo/v1/options/' ) ); return $response; }

Slide 27

Slide 27 text

Wrapping Up

Slide 28

Slide 28 text

Version 2.0 Beta 2 Release Date: May 28, 2015 https://github.com/WP-API/WP-API

Slide 29

Slide 29 text

https://speakerdeck.com/rachelbaker/utilizing-the-wp-rest-api Thank You! Slides: