One of the major features to be introduced in WordPress 3 was the ability to create custom post types. Basically, this lets you recreate the ‘Posts’ tab with specific functionality in mind. For example, creating a portfolio with fields for project images and client information. At Shockoe, we are using this in an upcoming project to that uses WordPress to provide content for a new iPhone app.

This makes WordPress much more viable as a CMS as opposed to blogging software that struggles to handle larger sites. In previous versions of WordPress this involved either a considerable amount of work or using a massive plugin. The best of those plugins, Magic Fields (a fork of Flutter), is several thousand lines of code. We can create a flexible solution in far fewer lines.

Code in action

add_action('init', 'post_type_example');
function post_type_example() {
'label' => __('Example'),
'public' => true,
'show_ui' => true,
'supports' => array(
'menu_position' => 5

register_taxonomy( ‘categories’, ‘example’, array( ‘hierarchical’ => true, ‘label’ => __(‘Categories’) ) );
register_taxonomy( ‘tags’, ‘example’, array(‘label’ => __(‘Tags’)));

Here we have created a new post type that will have it’s own sidebar tab under ‘Posts’ as well as fields for title, content (‘editor’), an excerpt, and comments. We also added functionality similar to WordPress’ tags and categories features by calling ‘register_taxonomy.’ Setting ‘hierarchal’ => true will make the field behave like categories while leaving it empty (or setting it to ‘false’) gives us the tags functionality.

That’s it. Simple. In a handful of lines we have a flexible solution that gives us the option of using better contextual grammar. That translates to a much better experience for clients and end users.

Adding Custom Functionality

The code above basically replicates the Posts functionality with the ability to have better labels. But what if we need to include custom information? For that we need add_meta_box:

add_action("admin_init", "admin_init");
function admin_init(){
add_meta_box("example_field", "Example Text Field", "example_field", "normal", "high");

function example_field () {
global $post;

$post_custom = get_post_custom($post->ID);
$text_field_content = $post_custom['text_field'][0];

print "


And to save our custom field’s information when we press ‘Publish’:

add_action('save_post', 'save_details');
function save_details(){
global $post;

update_post_meta($post->ID, ‘text_field’, $_POST[‘text_field’]);

Now we should have a custom text box that will appear below the content editor. When we save our post, the information in our custom field will be saved to the database. And when we edit our post, the text field will be repopulated.


With a little bit of coding knowledge you can create an elegant solution for turning WordPress into a functional CMS. And this is a very basic example; the possibilities of what can be created from this point are endless.

For more detailed information, see the register_post_type and add_meta_box reference pages in the WordPress Codex