[API] Registering A Custom Post Type With REST API Support full (ok)

https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/

<?php
/**
 * Register a book post type, with REST API support
 *
 * Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type
 */
/**
 * Register a book post type, with REST API support
 *
 * Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type
 */
add_action('init', 'my_book_cpt');
function my_book_cpt() {
  $labels = array(
    'name'               => _x('Books', 'post type general name', 'your-plugin-textdomain'),
    'singular_name'      => _x('Book', 'post type singular name', 'your-plugin-textdomain'),
    'menu_name'          => _x('Books', 'admin menu', 'your-plugin-textdomain'),
    'name_admin_bar'     => _x('Book', 'add new on admin bar', 'your-plugin-textdomain'),
    'add_new'            => _x('Add New', 'book', 'your-plugin-textdomain'),
    'add_new_item'       => __('Add New Book', 'your-plugin-textdomain'),
    'new_item'           => __('New Book', 'your-plugin-textdomain'),
    'edit_item'          => __('Edit Book', 'your-plugin-textdomain'),
    'view_item'          => __('View Book', 'your-plugin-textdomain'),
    'all_items'          => __('All Books', 'your-plugin-textdomain'),
    'search_items'       => __('Search Books', 'your-plugin-textdomain'),
    'parent_item_colon'  => __('Parent Books:', 'your-plugin-textdomain'),
    'not_found'          => __('No books found.', 'your-plugin-textdomain'),
    'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain'),
  );
  $args = array(
    'labels'                => $labels,
    'description'           => __('Description.', 'your-plugin-textdomain'),
    'public'                => true,
    'publicly_queryable'    => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'query_var'             => true,
    'rewrite'               => array('slug' => 'book'),
    'capability_type'       => 'post',
    'has_archive'           => true,
    'hierarchical'          => false,
    'menu_position'         => null,
    'show_in_rest'          => true,
    'rest_base'             => 'books',
    'rest_controller_class' => 'WP_REST_Posts_Controller',
    'supports'              => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
  );
  register_post_type('book', $args);
}
function my_plugin_rest_route_for_post($route, $post) {
  if ($post->post_type === 'book') {
    $route = '/wp/v2/books/' . $post->ID;
  }
  return $route;
}
add_filter('rest_route_for_post', 'my_plugin_rest_route_for_post', 10, 2);
?>

Ngoài ra, bạn có thể truyền một đối số cho rest_controller_class. Lớp này phải là một lớp con của WP_REST_Controller. Theo mặc định, WP_REST_Posts_Controller được sử dụng làm bộ điều khiển. Nếu bạn đang sử dụng bộ điều khiển tùy chỉnh, thì bạn có thể sẽ không ở trong không gian tên wp / v2.

Nếu bạn đang sử dụng rest_controller_class tùy chỉnh, thì REST API không thể tự động xác định tuyến đường cho một bài đăng nhất định. Trong trường hợp này, bạn có thể sử dụng bộ lọc rest_route_for_post để cung cấp thông tin này. Điều này cho phép loại bài đăng tùy chỉnh của bạn được định dạng đúng trong điểm cuối Tìm kiếm và cho phép các liên kết khám phá tự động.

Last updated