Setting up Custom Content Types using Stresslimit Custom Content Types plugin

When Custom Post Types (CPT) became available with the release of WordPress 3.0 everyone was super excited about this new features and many tutorials came out explaining how to use them. These were super helpful and helped us get started and I must admit, CPT have made building WordPress sites truly pleasurable.

Some folks did point out though, that to set these up, the code needed was quite cumbersome. Of course smart folks came up with handy tools and plugins to solve this.

I was reluctant at first and didn’t think that the extra code needed was that difficult to manage, but Joey introduced me to the Stresslimit Custom Content Types plugin and I’ve been hooked on it ever since. It’s super easy to use. The plugin is designed for developers, so it doesn’t simply work out of the box, but once installed all you do is add the following to your functions.php:

sld_register_post_type( 'your-post-type' );

This new function simply extends register_post_type().

So for example, if you wanted to create a new content type called Products, you would use:

sld_register_post_type( 'product' );

It also supports the same arguments as register_post_type() so if you wanted to override the default ones, you could use something like this:

$product_args = array(
	'supports' => array( 'title', 'thumbnail' ),
	'menu_icon' => get_template_directory_uri() . '/images/admin-product.png',
	'public' => false,
);
sld_register_post_type( 'slide', $product_args );

This function works perfectly and displays a nice custom content type in our admin panel with a new icon which we’ve added to our theme file.

Things do go wrong though if you’ve omitted to upload the plugin or it gets deactivated accidentally. If that happens, you get the following error on your site:

Fatal error: Call to undefined function sld_register_post_type() in ...

As a developer, that’s not a big issue. Most of us know how to read error messages and know how to fix them, but it’s much more troubling when clients accidentally get these messages. It’s happened to me in the past and I immediately got a phone call. An easy way to avoid this is to first do a check to see if the plugin is installed, then call the function. WordPress allows you to do this easily with function_exists(). The code for our Product CPT would thus be like so:

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

if (function_exists( 'sld_register_post_type' )) {

$product_args = array(
	'supports' => array( 'title', 'thumbnail' ),
	'menu_icon' => get_template_directory_uri() . '/images/admin-product.png',
	'public' => false,
);
sld_register_post_type( 'slide', $product_args );

}

That’s it. Next time you’re working on a site needing custom content type, check it out. It makes things much easier.