anurag-singh
5/25/2016 - 7:26 AM

Quickly register new taxonomy and CPT

	/**
	* Creates a new taxonomy for a custom post type
	*
	*/
	public function register_new_CPT_Taxo() {
		$cpt_name = 'Samhita';
		$sanitized_cpt = sanitize_title($cpt_name);

		$all_taxonomy = array('Veda');
		foreach ($all_taxonomy as $single) {

			$single = ucwords(strtolower(preg_replace('/\s+/', ' ', $single) ));

			$last_character = substr($single, -1);

			if ($last_character === 'y') {
			$plural = substr_replace($single, 'ies', -1);
			}
			else {
			$plural = $single.'s'; // add 's' to convert singular name to plural
			}

			$tax_slug = strtolower(str_replace(" ", "_", $single)); // Sanitize slug
			
			$opts['hierarchical'] = TRUE;
			//$opts['meta_box_cb'] = '';
			$opts['public'] = TRUE;
			$opts['query_var'] = $tax_slug;
			$opts['show_admin_column'] = TRUE;
			$opts['show_in_nav_menus'] = TRUE;
			$opts['show_tag_cloud'] = TRUE;
			$opts['show_ui'] = TRUE;
			$opts['sort'] = '';
			//$opts['update_count_callback'] = '';

			$opts['capabilities']['assign_terms'] = 'edit_posts';
			$opts['capabilities']['delete_terms'] = 'manage_categories';
			$opts['capabilities']['edit_terms'] = 'manage_categories';
			$opts['capabilities']['manage_terms'] = 'manage_categories';

			$opts['labels']['add_new_item'] = __( "Add New {$single}", $this->plugin_name );
			$opts['labels']['add_or_remove_items'] = __( "Add or remove {$plural}", $this->plugin_name );
			$opts['labels']['all_items'] = __( $plural, $this->plugin_name );
			$opts['labels']['choose_from_most_used'] = __( "Choose from most used {$plural}", $this->plugin_name );
			$opts['labels']['edit_item'] = __( "Edit {$single}" , $this->plugin_name);
			$opts['labels']['menu_name'] = __( $plural, $this->plugin_name );
			$opts['labels']['name'] = __( $plural, $this->plugin_name );
			$opts['labels']['new_item_name'] = __( "New {$single} Name", $this->plugin_name );
			$opts['labels']['not_found'] = __( "No {$plural} Found", $this->plugin_name );
			$opts['labels']['parent_item'] = __( "Parent {$single}", $this->plugin_name );
			$opts['labels']['parent_item_colon'] = __( "Parent {$single}:", $this->plugin_name );
			$opts['labels']['popular_items'] = __( "Popular {$plural}", $this->plugin_name );
			$opts['labels']['search_items'] = __( "Search {$plural}", $this->plugin_name );
			$opts['labels']['separate_items_with_commas'] = __( "Separate {$plural} with commas", $this->plugin_name );
			$opts['labels']['singular_name'] = __( $single, $this->plugin_name );
			$opts['labels']['update_item'] = __( "Update {$single}", $this->plugin_name );
			$opts['labels']['view_item'] = __( "View {$single}", $this->plugin_name );

			$opts['rewrite']['ep_mask'] = EP_NONE;
			$opts['rewrite']['hierarchical'] = FALSE;
			$opts['rewrite']['slug'] = __( $tax_slug, $this->plugin_name );
			$opts['rewrite']['with_front'] = FALSE;

			register_taxonomy( $tax_slug, $sanitized_cpt, $opts );
		}

		
		/**
		 * Create new custom post type
		*/

		 $cap_type = 'post';
		 $single = ucfirst($cpt_name);
		 $plural = $single.'s';

		 $opts['can_export'] = TRUE;
		 $opts['capability_type'] = $cap_type;
		 $opts['description'] = '';
		 $opts['exclude_from_search'] = FALSE;
		 $opts['has_archive'] = FALSE;
		 $opts['hierarchical'] = FALSE;
		 $opts['map_meta_cap'] = TRUE;
		 $opts['menu_icon'] = 'dashicons-admin-post';
		 $opts['menu_position'] = 25;
		 $opts['public'] = TRUE;
		 $opts['publicly_querable'] = TRUE;
		 $opts['query_var'] = TRUE;
		 $opts['register_meta_box_cb'] = '';
		 $opts['rewrite'] = FALSE;
		 $opts['show_in_admin_bar'] = TRUE; // Define For 'Top Menu' bar
		 $opts['show_in_menu'] = TRUE;
		 $opts['show_in_nav_menu'] = TRUE;
		 $opts['show_ui'] = TRUE;
		 $opts['supports'] = array( 'title', 'editor', 'thumbnail' );
		 $opts['taxonomies'] = array();
		 $opts['capabilities']['delete_others_posts'] = "delete_others_{$cap_type}s";
		 $opts['capabilities']['delete_post'] = "delete_{$cap_type}";
		 $opts['capabilities']['delete_posts'] = "delete_{$cap_type}s";
		 $opts['capabilities']['delete_private_posts'] = "delete_private_{$cap_type}s";
		 $opts['capabilities']['delete_published_posts'] = "delete_published_{$cap_type}s";
		 $opts['capabilities']['edit_others_posts'] = "edit_others_{$cap_type}s";
		 $opts['capabilities']['edit_post'] = "edit_{$cap_type}";
		 $opts['capabilities']['edit_posts'] = "edit_{$cap_type}s";
		 $opts['capabilities']['edit_private_posts'] = "edit_private_{$cap_type}s";
		 $opts['capabilities']['edit_published_posts'] = "edit_published_{$cap_type}s";
		 $opts['capabilities']['publish_posts'] = "publish_{$cap_type}s";
		 $opts['capabilities']['read_post'] = "read_{$cap_type}";
		 $opts['capabilities']['read_private_posts'] = "read_private_{$cap_type}s";
		 $opts['labels']['add_new'] = __( "Add New {$single}", $this->i18n );
		 $opts['labels']['add_new_item'] = __( "Add New {$single}", $this->i18n );
		 $opts['labels']['all_items'] = __( $plural, $this->i18n );
		 $opts['labels']['edit_item'] = __( "Edit {$single}" , $this->i18n);
		 $opts['labels']['menu_name'] = __( $plural, $this->i18n );
		 $opts['labels']['name'] = __( $plural, $this->i18n );
		 $opts['labels']['name_admin_bar'] = __( $single, $this->i18n );
		 $opts['labels']['new_item'] = __( "New {$single}", $this->i18n );
		 $opts['labels']['not_found'] = __( "No {$plural} Found", $this->i18n );
		 $opts['labels']['not_found_in_trash'] = __( "No {$plural} Found in Trash", $this->i18n );
		 $opts['labels']['parent_item_colon'] = __( "Parent {$plural} :", $this->i18n );
		 $opts['labels']['search_items'] = __( "Search {$plural}", $this->i18n );
		 $opts['labels']['singular_name'] = __( $single, $this->i18n );
		 $opts['labels']['view_item'] = __( "View {$single}", $this->i18n );
		 $opts['rewrite']['ep_mask'] = EP_PERMALINK;
		 $opts['rewrite']['feeds'] = FALSE;
		 $opts['rewrite']['pages'] = TRUE;
		 $opts['rewrite']['slug'] = __( strtolower( $plural ), $this->i18n );
		 $opts['rewrite']['with_front'] = FALSE;
		 
		 register_post_type( strtolower( $cpt_name ), $opts );
		
	}
	
	add_action('init', 'register_new_CPT_Taxo')