Opened 16 years ago
Closed 13 years ago
#12629 closed feature request (duplicate)
function to remove custom taxonomies and all their terms
| Reported by: | sillybean | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Taxonomy | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
I think there should be an easy way for plugins that create custom taxonomies to clean up after themselves on deactivation. I went over taxonomy.php pretty thoroughly and didn't find anything that does this directly.
The function below removes the custom taxonomies and terms from the various tables while leaving the built-in taxonomies alone.
<php
/*
assuming taxonomies for actor, director, and genre have been created on plugin activation...
*/
function remove_taxonomy($taxonomy) {
if (!$taxonomy->_builtin) {
global $wp_taxonomies;
$terms = get_terms($taxonomy);
foreach ($terms as $term) {
wp_delete_term( $term->term_id, $taxonomy );
}
unset($wp_taxonomies[$taxonomy]);
}
}
function deactivate_custom_taxes() {
remove_taxonomy('genre');
remove_taxonomy('actor');
remove_taxonomy('director');
remove_taxonomy('post_tag'); // this will fail silently
// do we need to flush the rewrite rules?
$GLOBALS['wp_rewrite']->flush_rules();
}
register_deactivation_hook( __FILE__, 'deactivate_custom_taxes' );
?>
If you like the idea, the remove_taxonomy() function could go into taxonomy.php, and then we'd document the deactivation procedure.
Change History (12)
#5
@
16 years ago
Oh, it makes perfect sense, and it's certainly better to nuke a lot of data on uninstall rather than deactivation, especially if a user is just being conscientious about upgrading. I'm just adding uninstall hooks to my mental list of plugin-related docs that need improvement. (It's a long one.)
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Data should generally be removed on an uninstall hook, not deactivation.