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

WP API (JSON REST API) at WordCamp Hampton Roads

WP API (JSON REST API) at WordCamp Hampton Roads

Nikhil Vimal

October 17, 2015
Tweet

More Decks by Nikhil Vimal

Other Decks in Programming

Transcript

  1. PUT

  2. GET

  3. $.ajax({ method: "post", url: ‘http://local.dev/wp-json/ wp/v2/posts', data: data, beforeSend: function

    ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce ); }, success : function( response ) { console.log( response ); alert( POST_SUBMITTER.success ); }, fail : function( response ) { console.log( response ); alert( POST_SUBMITTER.failure ); } });
  4. <?php function my_rest_prepare_post( $data, $post, $request ) { $_data =

    $data->data; $thumbnail_id = get_post_thumbnail_id( $post->ID ); $thumbnail = wp_get_attachment_image_src( $thumbnail_id ); $_data['featured_image_thumbnail_url'] = $thumbnail[0]; unset($_data['featured_image']); $data->data = $_data; return $data; } add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 );