WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 11 months ago

#12629 closed feature request (duplicate)

function to remove custom taxonomies and all their terms

Reported by: sillybean Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords:
Focuses: Cc:

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)

comment:1 nacin4 years ago

Data should generally be removed on an uninstall hook, not deactivation.

comment:2 sillybean4 years ago

  • Cc steph@… added

comment:3 follow-up: sillybean4 years ago

FWIW, I've never actually seen a plugin use an uninstall hook.

comment:4 in reply to: ↑ 3 scribu4 years ago

Related: #11058

Replying to sillybean:

FWIW, I've never actually seen a plugin use an uninstall hook.

I use it all the time in my plugins.

comment:5 sillybean4 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.)

comment:6 nacin4 years ago

  • Milestone changed from Unassigned to 3.1

We still don't have unregister_taxonomy() (#11058) and unregister_taxonomy_for_object_type(), so I'm thinking those would need to come before this one. Setting to 3.1.

comment:7 filosofo4 years ago

  • Owner filosofo deleted
  • Status changed from new to assigned

comment:8 kevinB4 years ago

  • Cc kevinB added

comment:9 F J Kaiser4 years ago

  • Cc 24-7@… added

+1

comment:10 nacin4 years ago

  • Milestone changed from Awaiting Triage to Future Release

comment:12 wonderboymusic11 months ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from assigned to closed

Duplicate of #11058.

Note: See TracTickets for help on using tickets.