Skip to content

Instantly share code, notes, and snippets.

@Oceas
Last active March 26, 2021 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oceas/4609bd66644b2273c34fcf9ee5d637e8 to your computer and use it in GitHub Desktop.
Save Oceas/4609bd66644b2273c34fcf9ee5d637e8 to your computer and use it in GitHub Desktop.
<?php
class Books {
/**
* Permalink slug for this post type
*
* @var string $slug Permalink prefix
* @since NEXT
*/
private $slug = 'cc_books';
/**
* Post ID of this post type.
*
* @var int $post_id Id of Post.
* @since NEXT
*/
private $post_id;
/**
* Construct
*
* @since 0.1.0
*/
public function __construct() {
$this->hooks();
}
/**
* Register Hooks
*
* @since NEXT
*/
private function hooks() : void {
add_action( 'init', [ $this, 'register_post' ] );
}
/**
* Post Labels
*
* @since NEXT
* @return array
*/
private function post_labels() : array {
return [
'name' => _x( 'Books', 'Post type general name', 'Book' ),
'singular_name' => _x( 'Book', 'Post type singular name', 'Book' ),
'menu_name' => _x( 'Books', 'Admin Menu text', 'Book' ),
'name_admin_bar' => _x( 'Book', 'Add New on Toolbar', 'Book' ),
'add_new' => __( 'Add New', 'Book' ),
'add_new_item' => __( 'Add New Book', 'Book' ),
'new_item' => __( 'New Book', 'Book' ),
'edit_item' => __( 'Edit Book', 'Book' ),
'view_item' => __( 'View Book', 'Book' ),
'all_items' => __( 'All Books', 'Book' ),
'search_items' => __( 'Search Books', 'Book' ),
'parent_item_colon' => __( 'Parent Books:', 'Book' ),
'not_found' => __( 'No Books found.', 'Book' ),
'not_found_in_trash' => __( 'No Books found in Trash.', 'Book' ),
'featured_image' => _x( 'Book Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'Book' ),
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'Book' ),
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'Book' ),
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'Book' ),
'archives' => _x( 'Book archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'Book' ),
'insert_into_item' => _x( 'Insert into Book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'Book' ),
'uploaded_to_this_item' => _x( 'Uploaded to this Book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'Book' ),
'filter_items_list' => _x( 'Filter Books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'Book' ),
'items_list_navigation' => _x( 'Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'Book' ),
'items_list' => _x( 'Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'Book' ),
];
}
/**
* Post Arguments
*
* @since NEXT
* @return array
*/
private function post_arguments() : array {
return [
'labels' => $this->post_labels(),
'description' => 'Book custom post type.',
'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' => 20,
'supports' => array( 'title', 'editor', 'author', 'thumbnail' ),
'taxonomies' => array( 'category', 'post_tag' ),
'show_in_rest' => true,
];
}
/**
* Register Post Type
*
* @since NEXT
*/
public function register_post() : void {
register_post_type( $this->slug, $this->post_arguments() );
}
}
new Books();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment