Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Nexus\PostType;
- class nx_PostType {
- protected $CPT_labels;
- protected $post_type;
- protected $post_name_singular;
- protected $post_name_plural;
- protected $taxonomy;
- // Register Custom Post Type
- public function nx_create_CPT() {
- register_post_type( $this->post_type, [
- 'label' => __( $this->post_name_singular, 'sage' ),
- 'labels' => $this->CPT_labels,
- 'supports' => ['title','editor','thumbnail'],
- 'taxonomies' => $this->taxonomy,
- 'hierarchical' => FALSE,
- 'public' => TRUE,
- 'show_ui' => TRUE,
- 'show_in_menu' => TRUE,
- 'menu_position' => 5,
- 'show_in_admin_bar' => TRUE,
- 'show_in_nav_menus' => TRUE,
- 'can_export' => TRUE,
- 'has_archive' => TRUE,
- 'exclude_from_search' => FALSE,
- 'publicly_queryable' => TRUE,
- 'capability_type' => 'post',
- ] );
- }
- //create the labels for the new post
- protected function nx_CPT_labels( $post_name_singular, $post_name_plural ) {
- $labels = [
- 'name' => _x( $post_name_singular, 'Post Type General Name', 'sage' ),
- 'singular_name' => _x( $post_name_plural, 'Post Type Singular Name', 'sage' ),
- 'menu_name' => __( $post_name_plural, 'sage' ),
- 'name_admin_bar' => __( $post_name_singular, 'sage' ),
- 'archives' => __( $post_name_singular . ' Archives', 'sage' ),
- 'attributes' => __( $post_name_singular . ' Attributes', 'sage' ),
- 'parent_item_colon' => __( 'Parent ' . $post_name_singular . ':', 'sage' ),
- 'all_items' => __( 'All ' . $post_name_plural, 'sage' ),
- 'add_new_item' => __( 'Add New ' . $post_name_singular, 'sage' ),
- 'add_new' => __( 'Add New', 'sage' ),
- 'new_item' => __( 'New ' . $post_name_singular, 'sage' ),
- 'edit_item' => __( 'Edit ' . $post_name_singular, 'sage' ),
- 'update_item' => __( 'Update ' . $post_name_singular, 'sage' ),
- 'view_item' => __( 'View ' . $post_name_singular, 'sage' ),
- 'view_items' => __( 'View ' . $post_name_plural, 'sage' ),
- 'search_items' => __( 'Search ' . $post_name_singular, 'sage' ),
- 'not_found' => __( 'Not found', 'sage' ),
- 'not_found_in_trash' => __( 'Not found in Trash', 'sage' ),
- 'featured_image' => __( 'Featured Image', 'sage' ),
- 'set_featured_image' => __( 'Set featured image', 'sage' ),
- 'remove_featured_image' => __( 'Remove featured image', 'sage' ),
- 'use_featured_image' => __( 'Use as featured image', 'sage' ),
- 'insert_into_item' => __( 'Insert into ' . $post_name_singular, 'sage' ),
- 'uploaded_to_this_item' => __( 'Uploaded to this ' . $post_name_singular, 'sage' ),
- 'items_list' => __( $post_name_plural . ' list', 'sage' ),
- 'items_list_navigation' => __( $post_name_plural . ' list navigation', 'sage' ),
- 'filter_items_list' => __( 'Filter ' . $post_name_plural . ' list', 'sage' ),
- ];
- return $labels;
- }
- //check if we need create new taxonomy
- protected function nx_have_to_create_taxonomy() {
- $new_taxonomy = [];
- foreach ($this->taxonomy as $taxonomy_item){
- if (!taxonomy_exists($taxonomy_item)){
- $new_taxonomy[] = $taxonomy_item;
- }
- }
- if ( count($new_taxonomy) > 0 ) {
- return $new_taxonomy;
- }
- return FALSE;
- }
- //Create new Taxonomy
- public function nx_new_taxonomy() {
- // Register Custom Taxonomy
- $taxonomies = $this->nx_have_to_create_taxonomy();
- if (!$taxonomies)
- return FALSE;
- foreach ( $taxonomies as $taxonomy ) {
- if (taxonomy_exists($taxonomy)){
- return FALSE;
- }
- $labels = $this->nx_CPT_labels($taxonomy,$taxonomy);
- register_taxonomy( $taxonomy, [ $this->post_type ], [
- 'labels' => $labels,
- 'hierarchical' => FALSE,
- 'public' => TRUE,
- 'show_ui' => TRUE,
- 'show_admin_column' => TRUE,
- 'show_in_nav_menus' => TRUE,
- 'show_tagcloud' => TRUE,
- ] );
- }
- }
- public function nx_register_taxonomy() {
- add_action( 'init', [ $this, 'nx_new_taxonomy' ], 0 );
- }
- //Call the action
- public function nx_CPT_action() {
- add_action( 'init', [ $this, 'nx_create_CPT' ], 0, 1 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement