Seattle WordPress Dev Meetup
February 2016
First WordPress Dev Meetup of 2016
This is our meetup...
Upcoming WordPress events
18 February | Contributor Meetup (Greenwood) |
23 February | Eastside WordPress Meetup |
5 March | Contributor Meetup (SLU) |
15 March | The Big Meetup is back! |
22 March | WP Developers: From Code to Client |
wpseattleslack.wordpress.com
Also, always learning!
Darren Krape
darrenkrape.com | twitter.com/dkrape |
API
Web browser
Application Programming Interface
REpresentational State Transfer
1. Access through resource types
JSON, HTML, etc.
2. Communicate through representations
api.example.com/post/3
api.example.com/user/7
api.example.com/todo/3/item/18
GET | Gets a resource |
POST | Creates a resource |
PUT | Updates a resource |
DELETE | Deletes a resource |
GET /wp/v2/posts/3 | Gets post 3 |
PUT /wp/v2/posts/3 | Updates post 3 |
DELETE /wp/v2/post/3 | Deletes (trashes) post 3 |
wired.com/wp-json
wired.com/wp-json/wp/v2/posts/1974761
darrenkrape.com/wp-json/wp/v2/comments/197508
{"id":197508,"post":957,"parent":0,"author":0,"author_name":"Whitney Krape","author_url":"http:\/\/www.whitneykrape.com\/","author_avatar_urls":{"24":"http:\/\/0.gravatar.com\/avatar\/06bd4d92401c74fba79aad89980b5efe?s=24&d=mm&r=g","48":"http:\/\/0.gravatar.com\/avatar\/06bd4d92401c74fba79aad89980b5efe?s=48&d=mm&r=g","96":"http:\/\/0.gravatar.com\/avatar\/06bd4d92401c74fba79aad89980b5efe?s=96&d=mm&r=g"},"date":"2015-06-04T15:56:00","date_gmt":"2015-06-04T20:56:00","content":{"rendered":"<p>Great post, very thorough and some solid tips that often get overlooked. The backpack comment “a jury-rigged day-pack” really stood out, shouldering that all day is serious pain. For my part a 60-65? liter was overkill in Japan, which was really glorified day trekking, but having the weight well distributed made a huge difference.<\/p>\n"},"link":"http:\/\/www.darrenkrape.com\/first-time-backpacker\/comment-page-1\/#comment-197508","status":"approved","type":"comment","_links":{"self":[{"href":"http:\/\/www.darrenkrape.com\/wp-json\/wp\/v2\/comments\/197508"}],"collection":[{"href":"http:\/\/www.darrenkrape.com\/wp-json\/wp\/v2\/comments"}],"up":[{"embeddable":true,"post_type":"post","href":"http:\/\/www.darrenkrape.com\/wp-json\/wp\/v2\/posts\/957"}]}}
var sasha = {
"age" : "6",
"species" : "cat",
"hometown" : "Seattle, WA",
"gender" : "female"
};
JavaScript Object Notation
Basically, JSON is a method to store information in an organized, easy-to-access manner
Text
{
"id": 197508,
"post": 957,
"parent": 0,
"author": 0,
"author_name": "Whitney Krape",
"author_url": "http://www.whitneykrape.com/",
"author_avatar_urls": {
"24": "http://0.gravatar.com/avatar/06bd4d92401c74fba79aad89980b5efe?s=24&d=mm&r=g",
"48": "http://0.gravatar.com/avatar/06bd4d92401c74fba79aad89980b5efe?s=48&d=mm&r=g",
"96": "http://0.gravatar.com/avatar/06bd4d92401c74fba79aad89980b5efe?s=96&d=mm&r=g"
},
"date": "2015-06-04T15:56:00",
"date_gmt": "2015-06-04T20:56:00",
"content": {
"rendered": "<p>Great post, very thorough and some solid tips that often get overlooked. The backpack comment “a jury-rigged day-pack” really stood out, shouldering that all day is serious pain. For my part a 60-65? liter was overkill in Japan, which was really glorified day trekking, but having the weight well distributed made a huge difference.</p>\n"
},
"link": "http://www.darrenkrape.com/first-time-backpacker/comment-page-1/#comment-197508",
"status": "approved",
"type": "comment",
"_links": {
"self": [
{
"href": "http://www.darrenkrape.com/wp-json/wp/v2/comments/197508"
}
],
"collection": [
{
"href": "http://www.darrenkrape.com/wp-json/wp/v2/comments"
}
],
"up": [
{
"embeddable": true,
"post_type": "post",
"href": "http://www.darrenkrape.com/wp-json/wp/v2/posts/957"
}
]
}
}
$(document).ready( function() {
$.ajax({
url: 'http://www.wired.com/wp-json/wp/v2/posts/',
type: "GET",
}).success( function( response ){
$.each( response, function( key, post ) {
$('#posts').append('<h3><a href="'+post.link+'">'+post.title.rendered+'</a></h3>');
});
});
});
/posts |
/pages |
/media |
/types Posts, pages, attachments, CPT ... |
/statuses Published, ... |
wired.com/wp-json/wp/v2/posts
/comments |
/taxonomies Categories, tags, custom taxonomies |
/categories |
/tags |
/users |
Arguments | Parameters |
author | [id] |
order | asc, desc |
orderby | date, id, include, title, slug |
per_page | [num] |
search | [text] |
wired.com/wp-json/wp/v2/posts/?per_page=1
&order=desc&orderby=title
/posts/?filter[posts_per_page]=1
/posts/?filter[orderby]=title&filter[order]=asc
You can also use WP_Query arguments
Get the plugin
add_action( 'init', 'my_book_cpt' );
function my_book_cpt() {
$args = array(
// Frontend
'has_archive' => false,
'public' => true,
// Admin
'capability_type' => 'post',
'supports' => array( 'editor', 'title' ),
// Rest
'show_in_rest' => true,
'rest_base' => 'books',
);
register_post_type( 'book', $args );
}
darrenkrape.com/wp-json/v2/wp/books
function get_author_recent_post( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
Custom endpoint callback
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v2', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_author_recent_post',
) );
} );
Registering route
darrenkrape.com/wp-json/v2/wp/author/1
Meta is in flux. See more on GitHub #1425 and Ryan McCue (lead dev)
// Unprotect ISBN field callback
public function unprotect_answers_field( $protected, $meta_key ) {
if( '_isbn' == $meta_key || '_isbn' == $meta_key && defined( 'REST_REQUEST' ) && REST_REQUEST ) {
$protected = false;
}
return $protected;
}
// Unprotect ISBN field
add_filter( 'is_protected_meta', 'unprotect_isbn_field' );
// Registering ISBN field
public function register_isbn_field() {
register_rest_field( 'question',
'_isbn',
array(
'get_callback' => array( $this, 'get_isbn_cb' ),
'update_callback' => null,
'schema' => null
)
);
}
// Callback to get ISBN field
public function get_isbn_cb( $object, $field_name, $request ) {
return get_post_meta( $object[ 'id' ], $field_name );
}
// Action for registering field
add_action( 'rest_api_init', array( $this, 'register_isbn_field' ) );
$(document).ready( function() {
$.ajax({
url: 'http://www.wired.com/wp-json/wp/v2/posts/?_jsonp=?',
type: "GET",
dataType: 'jsonp',
}).success( function( response ){
$.each( response, function( key, post ) {
$('#posts').append('<h3><a href="'+post.link+'">'+post.title.rendered+'</a></h3>');
});
});
});
add_action( 'rest_api_init', function() {
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
add_filter( 'rest_pre_serve_request', function( $value ) {
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Allow-Methods: GET' );
return $value;
});
}, 15 );
Reference: Josh Pollack (above code)
Cookie (client side)