WordPress custom taxonomies

One of my short term goals is to migrate a DokuWiki site over to WordPress. The decision to do this was somewhat complicated, but it came down to me not wanting to maintain too many systems. DokuWiki is great for what I used it for, but I'm maintaining seven websites now and I need to reduce my overhead since I need to focus on other things (writing, programming). I finished the second to last one (The Nobel Pen on Saturday and started into the one I've been stalling on.

Migrating my byline is actually a (relatively) major task. There are about a hundred short stories, a number of novels, and quite a few essays. In addition, almost everything is arranged into way too many taxonomies that make sense for that byline but not for anything else I do.

You might wonder why in the world I have so much tagging. Well, when I burn out I usually start cleaning. Plus, I wanted to see which views I wasn't writing about so I could. It ended up being a few months effort spread out since 1993, so it doesn't feel like I spent that much time on it.

WordPress handles custom taxonomies very well, but it needed some custom work. I could use a plugin to manage it, but after trying a few plugins, I decided to do it the "hard way." Now, the general suggestion is to do it in a custom theme, but I decided to create a plugin to manage it (local-sitename). This way, if I change the theme (which I have done twice now) I don't have to move over the taxonomies.

The basic local theme would go into wp-content/local-sitename/local-customizations.php. It needs a comment block which dictates what shows up on the plugin list inside WordPress.

/*
 Plugin Name: Local Customizations (sitename)
 Version: 0.0.0
 Author: D. Moonfire
 Description: Adds in the site-specific customizations for http://sitename/.
*/

The plugin file also has a hook which is called when the plugin is activated for a given site.

// Add in a hook to add the taxonomies and categories.
add_action("init", "sitename_init", 0);

function sitename_init() { // Register the taxonomies associated with pages. create_taxonomy(“format”, “formats”, “Format”, “Formats”, false); create_taxonomy(“language”, “languages”, “Language”, “Languages”, true); create_taxonomy(“magic”, “magic”, “Magic”, “Magic”, true); // quite a few more taxonomies

// We need pages to have archives so we can use the taxonomies. $page_type = get_post_type_object(“page”); $page_type->has_archive = true; }

The last parameter of create_taxonomy determines if it is a hierarchy taxonomy or a flat one. For format (e.g., Story, Essay, Novel), it doesn't make sense to have a tree-like structure. But for languages and magical systems (actually most of my taxonomies), I use the parent/child relationship since I kept track of related languages (Romance Languages verses French).

Creating the taxonomy is pretty simple. I have a helper function to do most of the hard work for me:

function create_taxonomy($key, $slug, $singular, $plural, $is_tree)
{
  register_taxonomy(
    $key,
    array("page"),
    array(
      "hierarchical" => $is_tree,
      "labels" => array(
        "name" => $plural,
        "singular_name" => $singular,
        "search_items" => "Search $plural",
        "popular_items" => "Popular $plural",
        "all_items" => "All $plural",
        "edit_item" => "Edit $singular",
        "update_item" => "Update $singular",
        "add_new_item" => "Add New $singular",
        "new_item_name" => "New $singular Name",
        "separate_items_with_commas" => "Separate $plural with commas",
        "add_or_remove_items" => "Add or remove $plural",
        "choose_from_most_used" => "Choose from most common $plural",
        "menu_name" => "$plural"
      ),
      "public" => true,
      "show_in_nav_menus" => true,
      "show_ui" => true,
      "query_var" => true,
      "rewrite" => array("slug" => $slug, "with_front" => false)));
}

This creates a pretty taxonomy. The new taxonomies show up under the "Pages" menu on the Dashboard and each one shows up on the right of each post. Now, you may notice that I have "page" in two places. Normally, taxonomies aren't associated with the "page" post type. Most of the examples, they have them assigned to custom post types.

For D. Moonfire, I used a custom post type instead of messing with page. Because of how WordPress works, a custom post type has to have a slug, which is why all my story URLs start with "/fiction/". For the D. Moonfire site, this way okay because I didn't have many links out there and I use a relatively flat story organization. For the other site, which has mostly kept unbroken links based on setting. I've also used much of that structure since 2002-2004, so I couldn't really use the "/fiction/". In this case, I used page directly. I did the same with Moonfire Games and the projects because of the historical links.

I try really hard not to break links. Even knowing that not many people visit my sites, I still feel the need for consistent URLs.

If you go to the website for MfGames Writing, you can see taxonomies in use on the right hand side. Clicking on the links brings up a list of pages with that category (say License). To get this to work, I had to use the $page_type->has_archive = true line above.

Metadata

Categories:

Tags: