unregister_taxonomy()WP 4.5.0

Unregisters a taxonomy.

Can not be used to unregister built-in taxonomies.

1 time — 0.000039 sec (very fast) | 50000 times — 0.89 sec (very fast) | PHP 7.0.5, WP 4.5
Hooks from the function

Return

true|WP_Error. True on success, WP_Error on failure or if the taxonomy doesn't exist.

Usage

unregister_taxonomy( $taxonomy );
$taxonomy(string) (required)
Taxonomy name.

Examples

0

#1 Delete taxonomy

Suppose during the event init we registered the taxonomy genre. And then later we need to cancel it.

// taxonomy registration
add_action( 'init', 'create_genre_taxonomy' );

function create_genre_taxonomy(){

  // Add tree taxonomy 'genre' (as categories)
  register_taxonomy( 'genre', array('book'), array(
	'hierarchical' => true,
	'labels' => $labels,
	'show_ui' => true,
	'query_var' => true,
	'rewrite' => array( 'slug' => 'genre' ),
  ) );
}

// deleting the taxonomy
add_action( 'init', 'unregister_genre_taxonomy', 99 );

function unregister_genre_taxonomy(){

	// cancel the tax only on individual pages
	if( ! is_singular() ){
		return;
	}

	unregister_taxonomy( 'genre' );
}

Notes

  • Global. WP_Taxonomy[]. $wp_taxonomies List of taxonomies.

Changelog

Since 4.5.0 Introduced.

unregister_taxonomy() code WP 6.4.3

function unregister_taxonomy( $taxonomy ) {
	global $wp_taxonomies;

	if ( ! taxonomy_exists( $taxonomy ) ) {
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
	}

	$taxonomy_object = get_taxonomy( $taxonomy );

	// Do not allow unregistering internal taxonomies.
	if ( $taxonomy_object->_builtin ) {
		return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed.' ) );
	}

	$taxonomy_object->remove_rewrite_rules();
	$taxonomy_object->remove_hooks();

	// Remove the taxonomy.
	unset( $wp_taxonomies[ $taxonomy ] );

	/**
	 * Fires after a taxonomy is unregistered.
	 *
	 * @since 4.5.0
	 *
	 * @param string $taxonomy Taxonomy name.
	 */
	do_action( 'unregistered_taxonomy', $taxonomy );

	return true;
}