It was quite apparent to me that the addition of custom post types made this version of WordPress the most powerful yet. When I first saw their power, I began adding custom post types immediately. One recent theme I developed took full advantage of them having a total of 3 custom post types. And then I wrote a plugin that used custom post types and custom taxonomies. It was soon apparent to me, I was repeating myself a lot and the code also began to become lengthy. The solution I came up with was to create a custom post type class.
Why A Class For Custom Post Types and Project Overview
Having a class to manage these will take full advantage of the reliability of object oriented PHP and made adding custom post types a mater of a few lines of code. You will do less coding because it was reusable.
To begin we going to do this in the context of creating a plugin. It will have several functions but one main functions will be to add a custom post type to manage our products.
Also the code we will be using for our custom post type class methods are found in a previous post on adding custom post types.
Registering Our Plugin
If you have not created a plugin before there are few steps but here it is short in sweet:
- Create a folder in your plugins directory called: our_products
- Inside that folder create a PHP file with the same name: our_products.php. See below:

- Next we are going to add the code to register the plugin:
/*
Plugin Name: Our Products
Plugin URI: http://www.wpinsideout.com
Author: Kyle G.
Author URI: http://www.wpinsideout.com
Description: Example project to show the use of a custom post type and custom taxonomy class
Version: 1.0
*/Now our plugin is registered with WordPress and if we go to the plugins tab in the admin we will see the plugin is available for activation.

If activate it nothing will happen. But as we code we can just refresh to see the changes our plugin is making.
Start Creating Our Class
We are now going to create a new PHP file in our plugin directory called: add_post_types.php. Inside this file we are going to create a class with the same name as the file. Here is the beginning code for the class:
<?php
class Add_post_types {
}
We are now going to need a few variables and a function that accepts an associative array to assign values to these variables. Here is our custom post type class so far:
<?php
class Add_post_types {
var $single; //this represents the singular name of the post type
var $plural; //this represents the plural name of the post type
var $type; //this is the actual type
function init($options){
foreach($options as $key => $value){
$this->$key = $value;
}
}
To understand what is going on here in our plugin we are going to pass an associative array with the keys having the same name as the classes variables. When we call the init method and pass our options array, this method will assign the values to the classes variables.
Now we need to create our first method to add the post type and we can take the example from our tutorial on custom post types. We want to take the function called “my_widget_post_type_init” and we can paste it directly into our class.
We are going to do several edits to this function. First, I am going to change its name to “add_post_type”. Then I am going replace every instance of the word “widget” in the labels array with “$this->single”. Then, I am going to replace every instance in the labels array of the word “widgets” with “$this->plural”. Lastly, I am going change “widget” in the register_post_type function to “$this->type”
If you are unfamiliar with object oriented PHP, using $this->single, $this->plural, or $this->type will take the class variables and use it in this method. It allows use to assign value to these variables in the init method and then be able to use it in this method. Here is the class so far with the edited functions
:
<?php
class Add_post_types {
var $single; //this represents the singular name of the post type
var $plural; //this represents the plural name of the post type
var $type; //this is the actual type
function init($options){
foreach($options as $key => $value){
$this->$key = $value;
}
}
function add_post_type(){
$labels = array(
'name' => _x($this->plural, 'post type general name'),
'singular_name' => _x($this->single, 'post type singular name'),
'add_new' => _x('Add ' . $this->single, $this->single),
'add_new_item' => __('Add New ' . $this->single),
'edit_item' => __('Edit ' . $this->single),
'new_item' => __('New ' . $this->single),
'view_item' => __('View ' . $this->single),
'search_items' => __('Search ' . $this->plural),
'not_found' => __('No ' . $this->plural . ' Found'),
'not_found_in_trash' => __('No ' . $this->plural . ' found in Trash'),
'parent_item_colon' => ''
);
$options = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
);
register_post_type($this->type, $options);
}
}
Next we are going to take the function called “widget_message” found in our custom post type tutorial. We are again to change a few things. First we are going to change the name of the function to “add_messages”. Second we are going to change the key in the messages array to “$this->type”. Third, we are going to replace every instance of “widget” with “$this->single” and every instance of “widgets” with “$this->plural”. Here is the completed class:
<?php
class Add_post_types {
var $single; //this represents the singular name of the post type
var $plural; //this represents the plural name of the post type
var $type; //this is the actual type
function init($options){
foreach($options as $key => $value){
$this->$key = $value;
}
}
function add_post_type(){
$labels = array(
'name' => _x($this->plural, 'post type general name'),
'singular_name' => _x($this->single, 'post type singular name'),
'add_new' => _x('Add ' . $this->single, $this->single),
'add_new_item' => __('Add New ' . $this->single),
'edit_item' => __('Edit ' . $this->single),
'new_item' => __('New ' . $this->single),
'view_item' => __('View ' . $this->single),
'search_items' => __('Search ' . $this->plural),
'not_found' => __('No ' . $this->plural . ' Found'),
'not_found_in_trash' => __('No ' . $this->plural . ' found in Trash'),
'parent_item_colon' => ''
);
$options = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
);
register_post_type($this->type, $options);
}
function add_messages ( $messages ) {
$messages[$this->type] = array(
0 => '',
1 => sprintf( __($this->single . ' updated. <a href="%s">View ' . $this->single . '</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __($this->single . ' updated.'),
5 => isset($_GET['revision']) ? sprintf( __($this->single .' restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __($this->single . ' published. <a href="%s">View ' . $this->single . '</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Book saved.'),
8 => sprintf( __($this->single . ' submitted. <a target="_blank" href="%s">Preview ' . $this->single . '</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __($this->single . ' scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview ' . $this->single . '</a>'),
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __($this->single . ' draft updated. <a target="_blank" href="%s">Preview ' . $this->single . '</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
}
Now that we have our class, we just need to add our post type in our main plugin file. Here are the steps:
- Include the file containing our class.
- Create a new instance of our class and assign it to a variable.
- Create our options array.
- Pass our options array to our init method.
- use the add_action function to hook onto the initialization of WordPress and call our add_post_type method through the use of a reference.
- We then need to use the add_filter function to filter ‘post_updated_messages’ using a reference to our class and method.
Here is the complete code below:
include('add_post_types.php');
$create_post_type = new Add_post_types;
$options = array(
'single' => 'Product',
'plural' => 'Products',
'type' => 'product'
);
$create_post_type->init($options);
add_action('init', array(&$create_post_type, 'add_post_type'));
add_filter('post_updated_messages', array(&$create_post_type, 'add_messages'));
That is it. Here is the result:

You see that the type has been added. All the labels are correct and the message display product. To really show you how quick and easy it is for you now, I’ll add a second for a custom post type of services. Here is the code:
include('add_post_types.php');
$create_post_type = new Add_post_types;
$options = array(
'single' => 'Product',
'plural' => 'Products',
'type' => 'product'
);
$create_post_type->init($options);
add_action('init', array(&$create_post_type, 'add_post_type'));
add_filter('post_updated_messages', array(&$create_post_type, 'add_messages'));
$services_post_type = new Add_post_types;
$options = array(
'single' => 'Service',
'plural' => 'Services',
'type' => 'service'
);
$services_post_type->init($options);
add_action('init', array(&$services_post_type, 'add_post_type'));
add_filter('post_updated_messages', array(&$services_post_type, 'add_messages'));
Now we get this:

Wrap Up On Using A Custom Post Type Class
You notice above that I did assign a new variable to create the second post type and that is only way it will work.
Overall, the coding is kept to a minimum. You can add custom post types fast and consistently. You can also take this class over to over projects and get a jump start on those projects. You can use this in your theme development. Having maybe a folder for your classes and include this file in your functions.php file. Then use your functions.php file to add the post types.






This is great, as is the following article about putting in a jquery datepicker.
However, I can’t get this to work. I follow your instructions to the letter and I get:
The plugin generated 201 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
Not sure what I am doing wrong here…?
Never mind! I was missing the php tags. Whoops
Great plugin!
I’m trying to edit the messages for the normal post but it doesn’t seem to work. I’m using this code in functions.php:
add_filter(‘post_updated_messages’, ‘my_updated_messages’);
function my_updated_messages( $messages ) {
global $post, $post_ID;
//$messages["post"][8] = ‘Message to be aproved in the following 24 hours.’;
return $messages;
}
Is there a mistake I don’t see or how could I just edit one of the messages?
(Without the comment // slashes.)
I figuered it out, it works. I added it in a if-then-else area and it was not adding the filter. Thanks.
Pingback: Adding Custom Post Type Counts To Your Dashboard | WP Inside Out
Thanks for such a well written article!