Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
* Plugin Name: WP REST API Demo
* Plugin URI: https://gist.github.com/wpscholar/693517420ca6c9e29e7719ef24e7e00f
* Description: A developer plugin designed for playing around with the WordPress REST API.
* Version: 1.0
* Author: Micah Wood
* Author URI: https://wpscholar.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: rest-api-demo
*/
function demo_register_post_type() {
register_post_type( 'demo-book', array(
'label' => esc_html__( 'Books', 'rest-api-demo' ),
'labels' => array(
'name' => esc_html_x( 'Books', 'post type general name', 'rest-api-demo' ),
'singular_name' => esc_html_x( 'Book', 'post type singular name', 'rest-api-demo' ),
'add_new' => esc_html_x( 'Add New', 'book', 'rest-api-demo' ),
'add_new_item' => esc_html__( 'Add New Book', 'rest-api-demo' ),
'edit_item' => esc_html__( 'Edit Book', 'rest-api-demo' ),
'new_item' => esc_html__( 'New Book', 'rest-api-demo' ),
'view_item' => esc_html__( 'View Book', 'rest-api-demo' ),
'view_items' => esc_html__( 'View Books', 'rest-api-demo' ),
'search_items' => esc_html__( 'Search Books', 'rest-api-demo' ),
'not_found' => esc_html__( 'No books found.', 'rest-api-demo' ),
'not_found_in_trash' => esc_html__( 'No books found in Trash.', 'rest-api-demo' ),
'all_items' => esc_html__( 'All Books', 'rest-api-demo' ),
'archives' => esc_html__( 'Book Archives', 'rest-api-demo' ),
'filter_items_list' => esc_html__( 'Filter books list', 'rest-api-demo' ),
'items_list_navigation' => esc_html__( 'Books list navigation', 'rest-api-demo' ),
'items_list' => esc_html__( 'Books list', 'rest-api-demo' ),
),
'public' => true,
'hierarchical' => false,
'has_archive' => true,
'menu_icon' => 'dashicons-book-alt',
'show_in_rest' => true,
'rest_base' => 'books',
//'rest_controller_class' => 'Demo_Book_API_Controller',
'rewrite' => array(
'slug' => 'books',
'with_front' => false,
),
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
'custom-fields',
'revisions',
),
) );
}
function demo_register_taxonomy() {
register_taxonomy( 'demo-book-genre', array(), array(
'labels' => array(
'name' => esc_html_x( 'Genres', 'taxonomy general name', 'rest-api-demo' ),
'singular_name' => esc_html_x( 'Genre', 'taxonomy singular name', 'rest-api-demo' ),
'menu_name' => esc_html_x( 'Genres', 'admin menu', 'rest-api-demo' ),
'all_items' => esc_html__( 'All Genres', 'rest-api-demo' ),
'edit_item' => esc_html__( 'Edit Genre', 'rest-api-demo' ),
'view_item' => esc_html__( 'View Genre', 'rest-api-demo' ),
'update_item' => esc_html__( 'Update Genre', 'rest-api-demo' ),
'add_new_item' => esc_html__( 'Add New Genre', 'rest-api-demo' ),
'new_item_name' => esc_html__( 'New Genre Name', 'rest-api-demo' ),
'not_found' => esc_html__( 'Not Found', 'rest-api-demo' ),
'no_terms' => esc_html__( 'No genres', 'rest-api-demo' ),
'items_list_navigation' => esc_html__( 'Genres list navigation', 'rest-api-demo' ),
'items_list' => esc_html__( 'Genres list', 'rest-api-demo' ),
'back_to_items' => esc_html__( 'Genres list navigation', 'rest-api-demo' ),
'parent_item' => esc_html__( 'Parent Genre', 'rest-api-demo' ),
'parent_item_colon' => esc_html__( 'Parent Genre:', 'rest-api-demo' ),
'search_items' => esc_html__( 'Search Genres', 'rest-api-demo' ),
),
'public' => true,
'hierarchical' => true,
'show_in_rest' => true,
'rest_base' => 'genres',
//'rest_controller_class' => 'Demo_Book_Genre_API_Controller',
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'books/genres',
'with_front' => false,
),
) );
}
register_activation_hook( __FILE__, function () {
demo_register_taxonomy();
demo_register_post_type();
flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, function () {
unregister_post_type( 'demo-book' );
unregister_taxonomy( 'demo-book-genre' );
flush_rewrite_rules();
} );
add_action( 'init', function () {
demo_register_taxonomy();
demo_register_post_type();
register_taxonomy_for_object_type( 'demo-book-genre', 'demo-book' );
register_meta( 'post', '_demo_book_author', [
'object_subtype' => 'demo-book',
'type' => 'string',
'description' => 'Book author name',
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => function ( $allowed, $name, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
'show_in_rest' => true,
] );
register_meta( 'term', '_demo_book_genre_type', [
'object_subtype' => 'demo-book-genre',
'type' => 'string',
'description' => 'Book genre type',
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => function ( $allowed, $name, $term_id ) {
return current_user_can( 'edit_term', $term_id );
},
'show_in_rest' => true,
] );
} );
add_action( 'rest_api_init', function () {
register_rest_field( 'demo-book', 'book_author', [
'get_callback' => function ( array $postdata, $field_name, WP_REST_Request $request, $post_type ) {
return get_post_meta( $postdata['id'], $field_name, true );
},
'update_callback' => function ( $value, WP_Post $post, $field_name, WP_REST_Request $request, $post_type ) {
return update_post_meta( $post->ID, $field_name, sanitize_text_field( $value ) );
},
'schema' => [
'description' => 'Book author name',
'type' => 'string',
'context' => [ 'view', 'edit' ],
],
] );
register_rest_field( 'demo-book-genre', 'book_genre_type', [
'get_callback' => function ( array $termdata, $field_name, WP_REST_Request $request, $post_type ) {
return get_term_meta( $termdata['id'], $field_name, true );
},
'update_callback' => function ( $value, WP_Term $term, $field_name, WP_REST_Request $request, $post_type ) {
return update_term_meta( $term->ID, $field_name, sanitize_text_field( $value ) );
},
'schema' => [
'description' => 'Book genre type',
'type' => 'string',
'context' => [ 'view', 'edit' ],
],
] );
register_rest_route( 'app/v1', '/books', [
'methods' => 'GET',
'callback' => function ( WP_REST_Request $request ) {
$books = [];
$query = new WP_Query( [
'post_type' => 'demo-book',
] );
if ( $query->have_posts() ) {
/**
* @var WP_Post $post
*/
foreach ( $query->posts as $post ) {
$books[] = [
'id' => $post->ID,
'title' => get_the_title( $post ),
'description' => get_the_excerpt( $post ),
];
}
}
return rest_ensure_response( $books );
}
] );
} );
class Demo_Book_API_Controller extends WP_REST_Posts_Controller {
public function __construct( $post_type ) {
parent::__construct( $post_type );
$this->namespace = 'book-plugin/v1';
}
}
class Demo_Book_Genre_API_Controller extends WP_REST_Terms_Controller {
public function __construct( $post_type ) {
parent::__construct( $post_type );
$this->namespace = 'book-plugin/v1';
}
}
add_filter( 'rest_prepare_demo-book', function ( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) {
//unset( $response->data['modified'] );
//$response->data['title'] = get_the_title( $post );
return $response;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.