Photocredit: berlin.wordcamp.org
WordCamp Berlin 2015
Access your site's data through an easy-to-use HTTP REST API
wp-api.org
Datenbank Operationen via HTTP (REST)
posts |
post meta |
post revision |
pages |
media |
comments |
taxonomies |
terms |
users |
Version 1.x (legacy):
https://domain.tld/wp-json/
Version 2.x beta (bald im Core):
https://domain.tld/wp-json/wp/v2
// Use this filter to filter output for posts.
// Change 'post' to whatever resource (user, media, comment, etc) to filter
add_filter( 'rest_prepare_post', 'remove_post_data', 10, 3 );
function remove_post_data( $data, $post, $request ) {
unset( $data->data['sticky'] ); // Don't show this!
return $data;
}
// Add fields to posts
add_action( 'rest_api_init', 'post_register_random_info' );
function post_register_random_info() {
register_api_field( 'post', // What api resource to target, post, page, user etc
'random_info', // Name of field
array(
'get_callback' => 'get_random_info', // Get callback
'update_callback' => null, // Put callback
'schema' => null,
)
);
}
// Just return some random data
function get_random_info( $object, $field_name, $request ) {
$data = array(
'data' => md5( rand( 0, 100000 ) ),
);
return $data;
}
// Add extra fields to user
add_action( 'rest_api_init', 'slug_register_user_info' );
function slug_register_user_info() {
register_api_field( 'user', // What api resource to target, post, page, user etc
'user_extra_info', // Name of field
array(
'get_callback' => 'get_user_extra_info', // Get callback
'update_callback' => null, // Put callback
'schema' => null,
)
);
}
// Callback for getting extra user info
function get_user_extra_info( $object, $field_name, $request ) {
$data = array(
'twitter' => get_the_author_meta( 'twitter', $object['id'] ),
'facebook' => get_the_author_meta( 'facebook', $object['id'] ),
'instagram' => get_the_author_meta( 'instagram', $object['id'] ),
);
return $data;
}
// Filter endpoints, removing those not wanted
add_filter( 'rest_endpoints', 'filter_endpoints', 20, 1 );
function filter_endpoints( $endpoints ) {
// Patterns available at the root of the api
$disable_endpoints = array(
'/wp/v2/media',
'/wp/v2/media/(?P<id>[\d]+)',
'/wp/v2/comments',
'/wp/v2/comments/(?P<id>[\d]+)',
);
foreach ( $disable_endpoints as $disable ) {
if ( isset( $endpoints[ $disable ] ) ) {
unset( $endpoints[ $disable ] );
}
}
return $endpoints;
}
// Register new route/endpoint
add_action( 'rest_api_init', function () {
register_rest_route(
'cb/v2', // Namespace for route/endpoint
'/myroute', // Route to be used, can contain regexp
array(
'methods' => 'GET', // Endpoint, get
'callback' => 'cb_get_myroute', // Call this function
'args' => array(), // Optional args, validation etc
)
);
} );
Teil 1:
rest_api_init
function cb_get_myroute( $args = null ) {
$posts = get_posts(
array(
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'rand',
)
);
$data = array(
'info' => date( 'Ymdhis', time() ) . '/' . time(),
'random_post' => $posts,
);
if ( empty( $posts ) ) {
return new WP_Error( 'cb_no_posts', 'No posts available!', array( 'status' => 404 ) );
}
return new WP_REST_Response( $data, 200 );
}
Teil 2:
Setze Argumente in Funktion
Photocredit: wptavern.com
http://shprink.github.io/wp-api-angularjs/
http://wphc.julienrenaux.fr/
function cb_add_book_args() {
global $wp_post_types;
$wp_post_types['book']->show_in_rest = true;
$wp_post_types['book']->rest_base = 'book';
$wp_post_types['book']->rest_controller_class = 'WP_REST_Posts_Controller';
}
add_action( 'init', 'cb_add_book_args', 30 );
function cb_add_cpts_to_api( $args, $post_type ) {
if ( 'movie' === $post_type ) {
$args['show_in_rest'] = true;
$args['rest_base'] = 'movie';
// $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
}
return $args;
}
add_filter( 'register_post_type_args', 'cb_add_cpts_to_api', 10, 2 );