CHAPTER 7 • Making the Most of WordPress Plugins 185
on the other hand, are additional post types, much like posts and pages, which are post
types themselves.
WHY USE A PLUGIN?
The reason for using a plugin for your custom taxonomies and custom post types is portabil-
ity. If you put this sort of functionality in your theme’s functions.php file (which is possible),
you will lose it when using a different theme, although you obviously can copy and paste it to
the new theme as well. It makes more sense to put things related to content rather than
design in a plugin so that it is portable across themes. Just keep the plugin activated, and
you’ll be fine.
You can read more about data portability in Chapter 8, “Plugins or functions.php?.”
CREATING A CUSTOM TAXONOMY
To create a custom taxonomy, use register_taxonomy() within a function and attach
it to the init hook. The settings for register_taxonomy() are all simple enough and
control how your taxonomy will be referred to in the admin interface, along with other
things such as if it should have its own permalink (the rewrite argument) and whether it is
to be hierarchical or not.
The following example creates a nonhierarchical taxonomy, which is to say that it will behave
much like the default tags, called Smashing Tags (depicted in Figure 7-3):
// Attach function to init hook
add_action( 'init', 'smashing_tax', 0 );
// Taxonomy function
function smashing_tax() {
// Register the taxonomy
register_taxonomy( 'smashing_taxonomy', 'post',
array(
'hierarchical' => false,
'labels' => array(
'name' => 'Smashing Tags',
'singular_name' => 'Smashing Tags',
'search_items' => 'Search Smashing Tags',
'popular_items' => 'Popular Smashing Tags',
'add_new_item' => 'Add New Smashing Tags'
),
'query_var' => true,
'rewrite' => true
)
);
}