HTML5 Zone is brought to you in partnership with:

I wrote my first program in Z80A when I was 14 on a ZX Spectrum and ever since I have been hooked. I love writing code of any flavour as well as being passionate about the coding process. I have worked professionally in the software industry for the last 15 years using Microsoft Technologies, and I code at night in PHP, HTML, Javascript and CSS. Chris is a DZone MVB and is not an employee of DZone and has posted 34 posts at DZone. You can read more from them at their website. View Full User Profile

De-registering a WordPress Custom Taxonomy

  • submit to reddit

Now if you have been following my posts on custom taxonomies How To Programatically Add Terms to a WordPress Taxonomy and How To Add A Custom Taxonomy To Your Theme, you will know how to register a custom taxonomy and how to register terms for that taxonomy. However what do you do if you want to de-register a taxonomy?

If you look for a function in the API or on the codex then you might be hard pressed to find one. However there is a way to do it. It does mean accessing variables in the WordPress framework in a way which is, effectively, unsupported but it can be done.

But more to the point why would you want to? Well let’s say you have a plugin which allows you to manage taxonomies. You can add new ones, add terms etc. If you update a taxonomy then you might need to unregister that taxonomy and then re-register it in order to display the changes that have been made.

Another example could be that you you have a very customised theme, and you want to remove the standard Tags and Categories taxonomies. You can also use the following technique to do that as well. So how does it work.

Well in WordPress taxonomies are held in a gloabl array called $wp_taxonomies, if you get access to this array using the global keyword you can then unset the value for the custom taxonomy and the taxonomy is no longer registered.

for example :-

global $wp_taxonomies;
	if( isset( $wp_taxonomies[ 'my_taxonomy' ] ) ) {
		unset( $wp_taxonomies[ 'my_taxonomy' ] );

Just be aware that as this is unsupported if the name of the $wp_taxonomies ever changed you would also need to update your plugin or theme code to follow.

Published at DZone with permission of Chris Odell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)