Skip to content

Instantly share code, notes, and snippets.

@kosso
Last active January 16, 2017 17:26
Show Gist options
  • Save kosso/8be491cb03d6b49d256a1a1d3d590918 to your computer and use it in GitHub Desktop.
Save kosso/8be491cb03d6b49d256a1a1d3d590918 to your computer and use it in GitHub Desktop.
Example Custom Post Type with REST API support
<?php
/**
* Plugin Name: Custom Post Types
* Description: Custom post types plugin.
* Plugin URI: http://kosso.co.uk
* Version: 1.0.0
* Author: Kosso
* Author URI: http://kosso.co.uk
* License: Free
* Network: true
*/
// Via docs: https://codex.wordpress.org/Post_Types
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}
// Via docs: https://developer.wordpress.org/rest-api/adding-rest-api-support-for-custom-content-types/
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
$post_type_name = 'acme_product';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment