Creating custom post types without a plugin

While I’m sure there are several plugins out there that will assist you in creating custom post types for your website, they don’t take too much code and can easily be made without a plugin. My system is fairly clean and modular – keep each post type within it’s own file, all post types within a single folder, and then call them once from the functions.php file.

Let’s create a custom post type called “Slides”. I often use this as a location to post home page slideshow images with captions – a simple area for customers to update down the road. Save this code to a file called slides.php inside a folder called /post-types/ in the root of your theme:

<?php
add_action('init', 'slides_register');
 
function slides_register() {
	$labels = array(
		'name' => _x('Slides', 'post type general name'),
		'singular_name' => _x('Slide', 'post type singular name'),
		'add_new' => _x('Add New', 'Slide'),
		'add_new_item' => __('Add New Slide'),
		'edit_item' => __('Edit slides'),
		'new_item' => __('New Slide'),
		'view_item' => __('View slides'),
		'search_items' => __('Search slides'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
        $args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => false,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'slides', 'with_front' => true ),
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array('title')
	  ); 
	register_post_type( 'slides' , $args );
}
// The category 
register_taxonomy("slide-category", array("slides"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true, 'show_admin_column' => true));
?>

Now let’s create another custom post type called Testimonials. Save this code to a file called testimonials.php in the /post-types/ directory:

<?php add_action('init', 'testimonials_register');
 
function testimonials_register() {
	$labels = array(
		'name' => _x('Testimonials', 'post type general name'),
		'singular_name' => _x('Testimonial', 'post type singular name'),
		'add_new' => _x('Add New', 'Testimonial'),
		'add_new_item' => __('Add New Testimonial'),
		'edit_item' => __('Edit testimonials'),
		'new_item' => __('New Testimonial'),
		'view_item' => __('View testimonials'),
		'search_items' => __('Search testimonials'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
        $args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => false,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'testimonials', 'with_front' => true ),
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array('title')
	  ); 
 
	register_post_type( 'testimonials' , $args );
}
// The category 
register_taxonomy("testimonial-category", array("testimonials"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true, 'show_admin_column' => true));
?>

You can rinse-and-repeat this as many times as you need. For each custom post type that you want, just create a new file within that folder and rename all the necessary fields.

Finally, include these functions through the functions.php file. Note the first two lines: this is the only location in this snippet of code that you would need to change. Adjust the file names and paths as needed, and add additional lines if you create even more custom post types.

require_once(TEMPLATEPATH . '/post-types/slides.php');
require_once(TEMPLATEPATH . '/post-types/testimonials.php');
// Do not change anything below this point
add_action("admin_init", "admin_init");
function admin_init(){}
add_action('save_post', 'save_details');
function save_details(){
    global $post;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
    } else {
    }
}

And now you should see the two custom post types appearing in the left WordPress nav bar:

Download the files here »