Download Personify, our first Genesis Framework Child Theme. It's FREE

More Info

Custom Post Type and Custom Taxonomy Code Generator

Custom Post Types (CPT) and Custom Taxonomies are great features that enable WordPress to be a great Content Management System (CMS) from just a blogging platform. I often use CPT and Custom Taxonomies in many projects to manage all posts.

If you’re new to CPT and Custom Taxonomies, be sure to read these two articles from the WordPress Codex:

When creating CPT or Custom Taxonomies, I realive I’ve been using the same code many times. Therefore, it don’t take too much time to create one. If you’re still new to these two great features of WordPress, I recommend using Custom Post Types and Custom Taxonomies Code Generator.

WordPress Code Generator

In few minutes and after few clicks, you’ll get the code to be used to create CPT and Custom Taxonomies. What you need to do is just to paste the code in your functions.php file or even better in your functioanlity plugin.

Here’s an example of the code generated from the tool:

<?php

/**
 * Project CPT
 *
 * @package WordPress
 * @subpackage Projects
 * @author Your Name <email@example.com>
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * @link http://www.blazersix.com/wordpress-code-generators/
 *
 * @todo Remove unneccessary code and update where needed.
 */

/**
 * Load the Project custom post type.
 */
add_action( 'init', 'afn_project_init' );

/**
 * Register Project custom post type.
 *
 * Registers the Project custom post type and related admin interface
 * elements (custom columns, update messages, metaboxes, standard menu and
 * screen icon CSS).
 * 
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function afn_project_init() {
	register_post_type( 'project', array(
		'exclude_from_search'    => false,
		'has_archive'            => false,
		'hierarchical'           => true,
		'labels'                 => array(
			'name'               => 'Projects',
			'singular_name'      => 'Project',
			'add_new'            => 'Add New',
			'add_new_item'       => 'Add New Project',
			'edit_item'          => 'Edit Project',
			'new_item'           => 'New Project',
			'view_item'          => 'View Project',
			'search_items'       => 'Search Projects',
			'not_found'          => 'No projects found.',
			'not_found_in_trash' => 'No projects found in Trash.',
			'parent_item_colon'  => 'Parent Projects:',
			'all_items'          => 'All Projects',
			'menu_name'          => 'Projects',
			'name_admin_bar'     => 'Project',
		),
		'menu_position'          => 50,
		'public'                 => true,
		'publicly_queryable'     => true,
		'register_meta_box_cb'   => 'afn_register_project_meta_boxes',
		'rewrite'                => array(
			'slug'               => 'project',
			'with_front'         => false
		),
		'show_ui'                => true,
		'show_in_menu'           => true,
		'show_in_nav_menus'      => false,
		'supports'               => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes', 'post-formats' )
	) );
	
	if ( is_admin() ) {
		add_filter( 'post_updated_messages', 'afn_project_updated_messages' );
		add_filter( 'manage_edit-project_columns', 'afn_project_register_columns' );
		add_action( 'manage_project_posts_custom_column', 'afn_project_display_columns', 10, 2 );
		add_action( 'save_post', 'afn_project_save_post', 10, 2 );
	}
}


/**
 * Project update messages.
 *
 * @see /wp-admin/edit-form-advanced.php
 *
 * @param array $messages The array of post update messages.
 * @return array An array with new CPT update messages.
 */
function afn_project_updated_messages( $messages ) {
	global $post;
	
	$messages['project'] = array(
		0  => '', // Unused. Messages start at index 1.
		1  => sprintf( 'Project updated. <a href="%s">View Project</a>', esc_url( get_permalink( $post->ID ) ) ),
		2  => 'Custom field updated.',
		3  => 'Custom field deleted.',
		4  => 'Project updated.',
		/* translators: %s: date and time of the revision */
		5  => isset( $_GET['revision'] ) ? sprintf( 'Project restored to revision from %s', wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
		6  => sprintf( 'Project published. <a href="%s">View Project</a>', esc_url( get_permalink( $post->ID ) ) ),
		7  => 'Project saved.',
		8  => sprintf( 'Project submitted. <a target="_blank" href="%s">Preview Project</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
		9  => sprintf( 'Project scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Project</a>',
		      // translators: Publish box date format, see http://php.net/date
		      date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) ), esc_url( get_permalink( $post->ID ) ) ),
		10 => sprintf( 'Project draft updated. <a target="_blank" href="%s">Preview Project</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
	);
	
	return $messages;
}


/**
 * Register custom Project columns.
 *
 * @param array $columns Column names to display.
 */
function afn_project_register_columns( $columns ) {
	// $columns['afn_custom_column_id'] = 'Column Name';
	
	return $columns;
}


/**
 * Display custom Project columns.
 *
 * @param string $column_id The id of the column to display.
 * @param int $post_id Post ID.
 */
function afn_project_display_columns( $column_id, $post_id ) {
	switch ( $column_id ) {
		case 'afn_custom_column_id' :
			// Output the custom column content here.
			echo 'Value';
			break;
	}
}

/**
 * Save Project CPT data.
 * 
 * @see wp_insert_post()
 * 
 * @param int $post_id Post ID.
 * @param object $post Post object.
 */
function afn_project_save_post( $post_id, $post ) {
	$is_autosave = ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ? true : false;
	$is_revision = wp_is_post_revision( $post_id );
	$is_valid_nonce = ( isset( $_POST['afn_project_nonce'] ) && wp_verify_nonce( $_POST['afn_project_nonce'], 'save-project_' . $post_id ) ) ? true : false;
	
	// Bail if the data shouldn't be saved or intention can't be verified.
	if ( $is_autosave || $is_revision || ! $is_valid_nonce ) {
		return;
	}
	
	// @todo Save custom data from $_POST global here.
}

/**
 * Register Project meta boxes.
 *
 * This is the callback defined in the Project CPT registration function.
 * Meta boxes or any other functionality that should be limited to the
 * Add/Edit Project screen and occurs after 'do_meta_boxes' can be
 * registered here.
 *
 * @param object $post The post being added or edited.
 */
function afn_register_project_meta_boxes( $post ) {
	// Update these parameters and uncomment to add a meta box
	// add_meta_box( 'meta-box-id', 'Project Meta Box', 'afn_project_meta_box', 'project', 'normal', 'default' );
}

/**
 * Display Project meta box.
 *
 * @param object $post The post being edited.
 * @param array $meta_box Arguments passed when the meta box was registered.
 */
function afn_project_meta_box( $post, $meta_box ) {
	wp_nonce_field( 'save-project_' . $post->ID, 'afn_project_nonce' );
	?>
	<p>
		<label for="field-id">Field Label:</label><br>
		<input type="text" name="field_name" id="field-id" value="<?php echo esc_attr( $field_value ); ?>" class="regular-text">
	</p>
	<p>
		<label for="field2-id">Field Label:</label><br>
		<textarea name="field2_name" id="field2-id" rows="3" class="widefat"><?php echo esc_textarea( $field2_value ); ?></textarea>
	</p>
	<?php
}

Although this code generator tool is great, it lacks in flexibility. Currently it only has very few options. However, it should give you a good starting point. From the code generated, you just need to do some minor changes to suit your need.

If you haven’t used CPT or Custom Taxonomies before, be sure to learn it. Once you know how to use them, I’m pretty sure that you will never look back.

Update: I’ve updated the example code generated above the latest code generated by the tool.

Trackbacks

  1. [...] types – Post, Page, Attachment, Revision and Navigation Menus. Before this, I also shared a WordPress Custom Post Types Generator to build your own custom post [...]

  2. [...] code. There are few tools available to generate the code for you. For example, take a look at the CPT Generator Tool we introduced before or the one by [...]

Leave a Comment

*