Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

De-registering a WordPress Custom Taxonomy

DZone's Guide to

De-registering a WordPress Custom Taxonomy

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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.

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Chris Odell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}