Skip to content

Instantly share code, notes, and snippets.

@alexwcoleman
Forked from jpurpleman/class.cpt.php
Last active September 19, 2017 19:44
Show Gist options
  • Save alexwcoleman/2fa146fc88fadafd7362a17f9e124339 to your computer and use it in GitHub Desktop.
Save alexwcoleman/2fa146fc88fadafd7362a17f9e124339 to your computer and use it in GitHub Desktop.
Custom Post Type Class
<?php
class MyCPTs {
public function __construct() {
add_action( 'init', array ( $this, 'load_cpts'), 0 );
}
/**
* Registers custom post type to hold the data of the custom texts
*/
public function load_cpts() {
$this->register_type( 'Books', 'books', 'books');
$this->register_type( 'Movies', 'movies', 'movies');
$this->register_type( 'Video Sessions', 'video_sessions', 'video_sessions');
}
/**
* Optimizing the creation of post types and allowing for flexibility
*
* @param string $text
* @param string $slug
* @param string $post_type
*/
public function register_type( $text, $slug, $post_type ) {
$labels = array(
'name' => _x( $text, 'General Name', $slug ),
'singular_name' => _x( $text, 'Singular Name', $slug ),
'menu_name' => __( $text . 's', $slug ),
'name_admin_bar' => __( $text, $slug ),
'archives' => __( $text . 's', $slug ),
'parent_item_colon' => __( $text . ':', $slug ),
'all_items' => __( 'All ' . $text, $slug ),
'add_new_item' => __( 'Add New ' . $text, $slug ),
'add_new' => __( 'Add New ' . $text, $slug ),
'new_item' => __( 'New Text for ' . $text, $slug ),
'edit_item' => __( 'Edit Text for ' . $text, $slug ),
'update_item' => __( 'Update Text for ' . $text, $slug ),
'view_item' => __( 'View Text for ' . $text, $slug ),
'search_items' => __( 'Search Text for ' . $text, $slug ),
'not_found' => __( 'Not found', $slug ),
'not_found_in_trash' => __( 'Not found in Trash', $slug ),
'featured_image' => __( 'Featured Image', $slug ),
'set_featured_image' => __( 'Set featured image', $slug ),
'remove_featured_image' => __( 'Remove featured image', $slug ),
'use_featured_image' => __( 'Use as featured image ', $slug ),
'insert_into_item' => __( 'Insert into Text for ' . $text, $slug ),
'uploaded_to_this_item' => __( 'Uploaded to this Text for ' . $text, $slug ),
'items_list' => __( 'Items list', $slug ),
'items_list_navigation' => __( 'Items list navigation', $slug ),
'filter_items_list' => __( 'Filter items list', $slug ),
);
$args = array(
'label' => __( $text, $slug ),
'description' => __( $text . 's Description', $slug ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'revisions', ),
'show_in_rest' => true,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-format-aside',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( $post_type, $args );
}
}
$my_cpts = new MyCPTs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment