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:

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:

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)

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:

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:

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.

Conclusion

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: http://www.dimoff.biz. facebook, twitter


Tutorial Categories: