Platinum Partner
css,html5,web design,tips and tricks

De-registering a WordPress Custom Taxonomy

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 {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}