WordPress Custom Post Type Registration using register_post_type

WordPress provide a way for their developer to add any kind of post type and all available options for it. This features is really handy to improve Backend User Experience so that user can add / update / delete any kind of post type easily.

Here is the code snippet I usually use to register a new wordpress custom post type with complete options. With this code you can add a new custom meta box, taxonomy, modify post type list table, and many more.

Explore!

<?php

/*
 **************************************************************
 * Custom Post Types - Testimonial
 **************************************************************
*/

$role_object = get_role( 'administrator' );
$role_object->add_cap( 'edit_testimonial' );
$role_object->add_cap( 'edit_testimonials' );
$role_object->add_cap( 'edit_others_testimonials' );
$role_object->add_cap( 'publish_testimonials' );
$role_object->add_cap( 'read_testimonial' );
$role_object->add_cap( 'read_private_testimonials' );
$role_object->add_cap( 'delete_testimonials' );
$role_object->add_cap( 'delete_others_testimonials' );


add_action('init', 'tcore_add_testimonial');
if ( !function_exists( 'tcore_add_testimonial' ) ) {
	function tcore_add_testimonial() 
	{
	  $labels = array(
		'name' => _x('Testimonials', 'post type general name', THEME_SLUG),
		'singular_name' => _x('Testimonial', 'post type singular name', THEME_SLUG),
		'add_new' => _x('Add New', 'infobox', THEME_SLUG),
		'add_new_item' => __('Add New Testimonial', THEME_SLUG),
		'edit_item' => __('Edit Testimonial', THEME_SLUG),
		'new_item' => __('New Testimonial', THEME_SLUG),
		'view_item' => __('View Testimonial', THEME_SLUG),
		'search_items' => __('Search Testimonials', THEME_SLUG),
		'not_found' =>  __('No testimonials found', THEME_SLUG),
		'not_found_in_trash' => __('No testimonials found in Trash', THEME_SLUG), 
		'parent_item_colon' => ''
	  );
	  $args = array(
		'labels' => $labels,
		'public' => false,
		'publicly_queryable' => false,
		'exclude_from_search' => true,
		'show_ui' => true, 
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'testimonial',
		'capabilities' => array(
			'edit_post' => 'edit_testimonial',
			'edit_posts' => 'edit_testimonials',
			'edit_others_posts' => 'edit_others_testimonials',
			'publish_posts' => 'publish_testimonials',
			'read_post' => 'read_testimonial',
			'read_private_posts' => 'read_private_testimonials',
			'delete_posts' => 'delete_testimonials',
			'delete_others_posts' => 'delete_others_testimonials',
		),
		'hierarchical' => false,
		'menu_icon' => 'dashicons-images-alt', /* https://developer.wordpress.org/resource/dashicons */
		'menu_position' => null,
		'supports' => array('title','editor','thumbnail',/*'comments','author','excerpt'*/)
	  ); 
	  register_post_type('tcore_testimonial',$args);
	}
}

/*
 **************************************************************
 * Map Meta Cap For Custom Post Type
 **************************************************************
*/

add_filter( 'map_meta_cap', 'testimonial_map_meta_cap', 10, 4 );

function testimonial_map_meta_cap( $caps, $cap, $user_id, $args ) {

	/* If editing, deleting, or reading a testimonial, get the post and post type object. */
	if ( 'edit_testimonial' == $cap || 'delete_testimonial' == $cap || 'read_testimonial' == $cap ) {
		$post = get_post( $args[0] );
		$post_type = get_post_type_object( $post->post_type );

		/* Set an empty array for the caps. */
		$caps = array();
	}

	/* If editing a testimonial, assign the required capability. */
	if ( 'edit_testimonial' == $cap ) {
		if ( $user_id == $post->post_author )
			$caps[] = $post_type->cap->edit_posts;
		else
			$caps[] = $post_type->cap->edit_others_posts;
	}

	/* If deleting a testimonial, assign the required capability. */
	elseif ( 'delete_testimonial' == $cap ) {
		if ( $user_id == $post->post_author )
			$caps[] = $post_type->cap->delete_posts;
		else
			$caps[] = $post_type->cap->delete_others_posts;
	}

	/* If reading a private testimonial, assign the required capability. */
	elseif ( 'read_testimonial' == $cap ) {

		if ( 'private' != $post->post_status )
			$caps[] = 'read';
		elseif ( $user_id == $post->post_author )
			$caps[] = 'read';
		else
			$caps[] = $post_type->cap->read_private_posts;
	}

	/* Return the capabilities required by the user. */
	return $caps;
}

/*
 **************************************************************
 * Custom Taxonomy : Testimonial Categories
 **************************************************************
*/

add_action( 'init', 'testimonial_taxonomies' );

function testimonial_taxonomies() {

	register_taxonomy(
		'testimonial_category',
		array( 'tcore_testimonial' ),
		array(
			'public' => true,
			'labels' => array( 'name' => 'Testimonial Categories', 'singular_name' => 'Testimonial Category' ),
			'hierarchical' => true
		)
	);
}


/*
 **************************************************************
 * Custom Post Field
 **************************************************************
*/

add_action( 'cmb2_admin_init', 'tcore_testimonial_metabox' );

function tcore_testimonial_metabox() {

	// Start with an underscore to hide fields from custom fields list
	$prefix = '_tcore_testimonial_';

	/**
	 * Sample metabox to demonstrate each field type included
	 */
	$tcore_testimonial = new_cmb2_box( array(
		'id'            => $prefix . 'metabox',
		'title'         => __( 'Testimonial Data', 'cmb2' ),
		'object_types'  => array( 'tcore_testimonial' )
	) );

	$tcore_testimonial->add_field( array(
		'name'       => __( 'Author Short Description', 'cmb2' ),
		'desc'       => __( 'Testimonial Author Short Description', 'cmb2' ),
		'id'         => $prefix . 'author_desc',
		'type'       => 'textarea',
		
	) );

}


/*
 **************************************************************
 * Custom columns for custom post types
 **************************************************************
*/

add_filter( 'manage_edit-tcore_testimonial_columns', 'my_edit_testimonial_columns' ) ;

function my_edit_testimonial_columns( $columns ) {

	$columns = array(
		'cb' => '<input type="checkbox" />',
		'title' => __( 'Author' ),
		'content' => __( 'Testimonial' ),
		'description' => __( 'Author Description' ),
		'category' => __( 'Category' ),
		'date' => __( 'Date' )
	);

	return $columns;
}

add_action( 'manage_tcore_testimonial_posts_custom_column', 'my_manage_testimonial_columns', 10, 2 );

function my_manage_testimonial_columns( $column, $post_id ) {
	global $post;

	switch( $column ) {

		case 'content' :
			/* Get the post meta. */
			$content = get_the_content( $post_id );
			/* If no duration is found, output a default message. */
			if ( empty( $content ) )
				echo __( 'No testimonial yet.' );

			/* If there is a duration, append 'minutes' to the text string. */
			else
				echo $content;

			break;
			
		case 'description' :
			/* Get the post meta. */
			$desc = get_post_meta( $post_id, '_tcore_testimonial_author_desc', true );
			/* If no duration is found, output a default message. */
			if ( empty( $desc ) )
				echo __( 'No author description' );

			/* If there is a duration, append 'minutes' to the text string. */
			else
				echo $desc;

			break;
	
		case 'category' :

			/* Get the genres for the post. */
			$terms = get_the_terms( $post_id, 'testimonial_category' );

			/* If terms were found. */
			if ( !empty( $terms ) ) {

				$out = array();

				/* Loop through each term, linking to the 'edit posts' page for the specific term. */
				foreach ( $terms as $term ) {
					$out[] = sprintf( '<a href="%s">%s</a>',
						esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'testimonial_category' => $term->slug ), 'edit.php' ) ),
						esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'testimonial_category', 'display' ) )
					);
				}

				/* Join the terms, separating them with a comma. */
				echo join( ', ', $out );
			}

			/* If no terms were found, output a default message. */
			else {
				_e( 'No Category' );
			}

			break;

		/* Just break out of the switch statement for everything else. */
		default :
			break;
	}
}

/* Make Category Sortable */

add_filter( 'manage_edit-tcore_testimonial_sortable_columns', 'my_testimonial_sortable_columns' );

function my_testimonial_sortable_columns( $columns ) {

	$columns['category'] = 'category';

	return $columns;
}


?>

Leave A Comment

%d bloggers like this: