Advertisement

Using SuperCPT to Create Custom Post Types, Taxonomies and Meta Boxes

by
Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

With the release of version 3.0, WordPress introduced custom post types and updated custom taxonomies which were introduced in v2.8. Since then, WordPress users and developers are able to create their own post types and taxonomies. People are no longer chained to the dull "posts" and "pages" with the boring "categories" and "tags".

Yes, using custom post types, custom taxonomies and custom meta boxes are cool. You know what's cooler? Creating each with a single line of code.


The Hassle of Creating Custom Post Types and Taxonomies

I find it hard and boring to create custom post types and taxonomies with the register_post_type() and register_taxonomy() functions. While they're extremely useful, I kind of hate it when I have to write a bunch of arguments and pass them with a function which I also have to use with the add_action() function.

Even as a developer, it's painful to do this with every single post type and taxonomy I have to create - I can't imagine how hard it is for novice WordPress users.

Don't get me wrong, you still can or have to do them with SuperCPT, too - but SuperCPT embraces the DRY (Don't Repeat Yourself) principle and eases up the process. Practically, you just have to set the lowercase "singular name" of your post type or taxonomy and SuperCPT handles the rest with proper capitalization.

And the best part is, you don't have to deal with the mess of creating custom meta boxes and meta fields in them!

SuperCPT lets you create meta boxes with fields that vary between simple HTML elements from WYSIWYG editors and date pickers. Letting us do all of these by writing simple arrays is by far the easiest way I've ever seen for creating meta boxes.


Using SuperCPT, the All-in-One Solution

I can praise SuperCPT all day but as the creator Matthew Boynes said on SuperCPT's GitHub page, the proof is in the pudding and we should get to the examples - right after we see an awesome, five minute screencast.

Grab a copy from the WordPress.org Plugin Repository, install it and then, open your theme's functions.php file to work SuperCPT's magic!

The Screencast

http://vimeo.com/59368054

Creating Custom Post Types

As you can see on the video, creating custom post types is as easy as typing a line of code:

<?php

// the regular method
/*
register_post_type( 'computer_part',
	array(
		'labels' => array(
			'name' => __( 'Computer Parts', 'my-locale' ),
			'singular_name' => __( 'Computer Part', 'my-locale' )
		),
	'public' => true,
	'has_archive' => true,
	)
);
*/

// the SuperCPT method
$type_computer_part = new Super_Custom_Post_Type( 'computer-part' );

?>

SuperCPT automatically takes "computer-part" and removes any hyphens and underscores and capitalizing each word. It also sets the "singular" and "plural" forms of "computer-part" and uses them to form the labels, like "Add New Computer Part" or "Search Computer Parts" and so on.

Of course, you can set any labels as you like. In fact, you can set all the arguments that register_post_type() has, if you like to play around. Head over to SuperCPT Wiki's "Custom Post Types" section to find out the whole list of arguments and their default values.

SuperCPT includes a free icon set called Glyphicons for you to set icons for your post types. It's as easy as typing another line of code: $type_computer_part->set_icon( 'display' ); If you wonder what to use as the parameter, check the SuperCPT page under your admin panel's Tools - you'll find the names of the icons.

Creating Custom Taxonomies

Creating custom taxonomies with SuperCPT is a lot like creating custom post types. Again, a single line is enough for SuperCPT:

<?php

// the regular method
/*
register_taxonomy(
	'manufacturers',
	'computer-part',
	array(
		'label' => __( 'Manufacturers', 'my-locale' ),
		'rewrite' => array( 'slug' => __('manufacturer', 'my-locale') )
	)
);
*/

// the SuperCPT method
$tax_manufacturer = new Super_Custom_Taxonomy( 'manufacturer' );

?>

As with the custom post types, custom taxonomies' arguments are automatically set, but can also be changed. Check SuperCPT Wiki's "Custom Post Types" section to find out more about arguments.

As you can guess: this one line of code above can't connect your custom taxonomy to a post type. You need to do that with a helper function of SuperCPT, like this: $tax_manufacturer->connect_post_types( 'computer-part' );

Creating Custom Meta Boxes

Buckle up, this is my favorite part! :)

Seriously, the best part about SuperCPT is the easiness of adding custom post meta boxes. You don't have to mess with any HTML codes or worry about your data being saved, sanitized and whatnot. Though, we can't use single line codes to create meta boxes this time (obviously).

There are two essential functions: add_meta_box() and add_meta_boxes(). (We're going to stick with the first one for this tutorial.) The id and fields parameters are required but there are a bunch of other parameters on the wiki if you'd like to see.

<?php

$type_computer_part->add_meta_box( array(
	'id' => 'our-review', // the title is converted from 'id', if there's no 'title' parameter
	'fields' => array(
		'score' => array( 'label' => __( 'Our Score', 'my-locale' ), 'type' => 'radio', 'options' => array( '1', '2', '3', '4', '5' ) ),
		'release-date' => array( 'label' => __( 'Release Date', 'my-locale' ), 'type' => 'date' ), // yes, there is a date picker...
		'short-comment' => array( 'label' => __( 'A Short Comment', 'my-locale' ), 'type' => 'textarea' ),
		'review-text' => array( 'label' => __( 'Full Review', 'my-locale' ), 'type' => 'wysiwyg' ) // ...and there's a WYSIWYG text area! :)
	)
) );

?>

Now, I know there are lots of arrays, but there are things that even SuperCPT can predict, right? :)

There are lots of field elements you can place into the meta boxes:

  1. Hidden boolean fields
  2. Regular text inputs
  3. Regular textareas
  4. WYSIWYG editors
  5. Checkboxes
  6. Radio boxes
  7. Select boxes
  8. Date pickers
  9. And the new HTML5 input fields like: tel, email etc.
We can also pull data from other custom post types and fill the checkbox, radio box or select box fields with this data. This can be very useful for some cases!

The Wrapper Function

One last thing: You need a wrapper function to set these custom post types and hook that function to an action - SuperCPT recommends the after_setup_theme hook but if you're going to write a plugin to create the custom post types and taxonomies, you better use the hook plugins_loaded to wait for the plugins to load. You also need to be sure the Super_Custom_Post_Type class exists before referencing it.

Here's the demo wrapper function from the SuperCPT documentation:

<?php

function scpt_demo() {
	if ( ! class_exists( 'Super_Custom_Post_Type' ) )
		return;

	// All your SuperCPT magic goes here!
}
add_action( 'after_setup_theme', 'scpt_demo' );

?>

Conclusion

There, now you know all about the easiest custom post type and taxonomy manager, SuperCPT! For further information, check out the full documentation. There are a bunch of helper functions, actions and filters you may like.

What do you think about this cool WordPress plugin? Tell us what you think by commenting below, and if you liked it (I'm pretty sure you will!), don't forget to share the post!

Advertisement