frankgresslin
11/7/2017 - 6:56 PM

register_post_type

Register a custom post type

<?php

if ( ! function_exists( 'fg_project_post_type' ) ) :

	/**
	 * 	Register Custom Post Type 
	 *
	 * 	@return  array  $args    
	 *	@link    hhttps://codex.wordpress.org/Function_Reference/register_post_type
	 */

	function fg_project_post_type() {
	
	  $labels = array(
	    'name'               		=> _x( 'Projects', 'post type general name', 'include' ),
	    'singular_name'      		=> _x( 'Project', 'post type singular name', 'include' ),
	    'menu_name'          		=> _x( 'Projects', 'admin menu', 'include' ),
	    'name_admin_bar'     		=> _x( 'Project', 'add new on admin bar', 'include' ),
	    'add_new'            		=> _x( 'Add New', 'project', 'include' ),
	    'add_new_item'       		=> __( 'Add New Project', 'include' ),
	    'new_item'           		=> __( 'New Project', 'include' ),
	    'edit_item'          		=> __( 'Edit Project', 'include' ),
	    'view_item'          		=> __( 'View Project', 'include' ),
	    'all_items'          		=> __( 'All Projects', 'include' ),
	    'search_items'       		=> __( 'Search Projects', 'include' ),
	    'parent_item_colon'  		=> __( 'Parent Projects:', 'include' ),
	    'not_found'          		=> __( 'No projects found.', 'include' ),
	    'not_found_in_trash' 		=> __( 'No projects found in Trash.', 'include' ),
	  );
	
	  $args = array(
	    'labels'					      => $labels,
	    'description'				    => __( 'List of Projects', 'include' ),
	    'public'				      	=> true,
	    'exclude_from_search'		=> false,
	    'publicly_queryable'		=> true,
	    'show_ui'					      => true,
	    'show_in_menu'				  => true,
	    'show_in_nav_menus'			=> true,
	    'show_in_admin_bar'			=> true,
	    'query_var'					    => true,
	    'rewrite'					      => array( 'slug' => 'project' ),
	    'capability_type'			  => 'project',
	    'capabilities' 				  => array(
			  'publish_posts' 		  => 'publish_projects',
			  'edit_posts' 			    => 'edit_projects',
			  'edit_others_posts' 	=> 'edit_others_projects',
				'delete_posts' 			  => 'delete_projects',
				'delete_others_posts' => 'delete_others_projects',
				'read_private_posts' 	=> 'read_private_projects',
				'edit_post' 			    => 'edit_project',
				'delete_post' 			  => 'delete_project',
				'read_post' 			    => 'read_project',
				),
	    'has_archive'			 	    => true,
	    'hierarchical'				  => false,
	    'menu_position'				  => 12,
	    'menu_icon'					    => 'dashicons-tagcloud',
		'menu_icon'					      => get_stylesheet_directory_uri() . '/assets/admin/images/generic.png',
		'supports'					      => array( 'title','editor','author','thumbnail','excerpt','comments','revisions','custom-fields','post-formats')
	  );
	
	  register_post_type( 'project', $args );
	}
	
	add_action( 'init', 'fg_project_post_type' );

endif;


if ( ! function_exists( 'fg_project_rewrite_flush' ) ) :

	/**
	 * 	Flushes WordPress rewrite rules 
	 *	@link  https://codex.wordpress.org/Function_Reference/flush_rewrite_rules
	 */

	function fg_project_rewrite_flush() {
	    flush_rewrite_rules();
	}
	
	add_action( 'after_switch_theme', 'fg_project_rewrite_flush' );

endif;