May 28, 2015 9:01 am

How to Create a Plugin with a Custom Taxonomy WordPress

Creating plugins is a much better way to structure your functionality than hard coding the functionality in the theme when we are not talking about display logic. Plugins are portable, enabling your clients to still use the functionality you have created for their website even if they decide to switch themes. Furthermore, you may create must-use plugins (by placing the plugin in wp-content/mu-plugins) which would disallow your clients from incidentally deactivating or deleting the plugin. Also, they allow you to use that functionality with ease in another project you are working on among many other benefits.

How to Create a Plugin with a Custom Taxonomy WordPress

What will we make?

A plugin that we can activate:

How to Create a Plugin with a Custom Taxonomy WordPress Plugin View

Our own custom taxonomy:

How to Create a Plugin with a Custom Taxonomy WordPress Taxonomy Admin

Which gets shown to users:

How to Create a Plugin with a Custom Taxonomy WordPress Zookeeper Post

and whose archives we can browse:

How to Create a Plugin with a Custom Taxonomy WordPress Zookeper Archive

The Plugin

Firstly, we create a file in wp-content/plugins/ called zookeepers.php. Then we add our plugin’s metadata:

Plugin Name: Taxonomy Zookepers
Plugin URI:
Description: This plugin adds a zookepers taxonomy that let you assign zookeepers to posts.
Author: PHP Gang
Version: 0.1
Author URI:
License: GPL2

We define the name of the plugin, the link to the plugin’s page, a description of the plugin, author, version, the author’s website and the license. This will be shown when we are in the Plugins page (Plugins) in our Admin dashboard.

Then, we start adding code to our file:

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

class taxonomyZookepers {
    public function __construct() {
        add_action("init", array($this, 'init'));
          load_plugin_textdomain('taxonomy-zookeepers', FALSE, dirname(plugin_basename(__FILE__)).'/languages/');

We create a class which when instantiated will hook itself to the initialization of WordPress and execute the $this->init method and we prepare our plugin for localization.

We define our init function and create an array with labels (those will be used in the user interface and the admin dashboard for the different actions that our taxonomy supports and represent the text that will be shown to the users)

public function init() {
        $labels = array(
            'name' => __('Zookeepers', "taxonomy-zookepers"),
            'singular_name' => __("Zookeeper", "taxonomy-zookepers"),
            'all_items' => __("All Zookeepers", "taxonomy-zookepers"),
            'edit_item' => __("Edit Zookeeper", "taxonomy-zookepers"),
            'view_item' => __('View Zookeeper', "taxonomy-zookepers"),
            'update_item' => __("Update Zookeeper", "taxonomy-zookepers"),
            'add_new_item' => __("Add New Zookeeper", "taxonomy-zookepers"),
            'separate_items_with_commas' => __("Separate zookeepers with commas", "taxonomy-zookepers"),
            'popular_items' => __("Popular Zookeepers", "taxonomy-zookepers")


Note that we have enclosed our strings in __() which faciittes localization in WordPress. This would allow other people to localize our plugin to their own language, The second argument is a unique string with the domain/namespace that your plugin would use for the translation purposes. If you are echoing something directly to the screen you should use _e() instead for localization.

Then, we register our new taxonomy:

register_taxonomy("zookeeper", array("post", "page"), array(
            "labels" => $labels,
            'hierarchical' => false,
            'public' => true,
            'show_in_ui' => true,
            'rewrite' => array("slug" => 'zookeeper')


We name it zookeeper*, attach it to posts and pages, pass the labels that we defined before, declare that it is not hierarchical (if hierarchical was true the taxonomy would become like a category and now it would operate like a tag), we say we want it to be public (visible both in the admin dashboard and the front-end) and we declare we want the slug of the taxonomy to be zookeeper.

Finally, we instantiate our class – new taxonomyZookepers();

To display our newly created taxonomy to our front-end we make a small change:

if ( is_single() ) :
                the_title( '<h1 class="entry-title">', '</h1>' );
                the_terms(get_the_ID(), 'zookeeper',"<p class='zoo-watch'>Zookeepers at watch: ", ', ',"</p>" );

We add a call to the_terms() with the post id (we are inside The Loop so we can use get_the_ID() and we display the terms for the taxonomy zookeeper – we add before them a paragraph, separate them with commas and close the paragraph in the end.

Now, all we have to do is go to Settings -> Permalinks to refresh the changes we made and you could see our taxonomy in action.


I hope you have seen that creating a plugin is not a hard job. All you need to do is hook yourself into the WordPress API and add your custom functionality. When deciding whether to add some functionality for a client to his theme or to a plugin – always think about this: is this some sort of display logic (theme) or brand-new functionality that the project requires (plugin).

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: facebook, twitter

Tutorial Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *