[wp-trac] [WordPress Trac] #12629: function to remove custom taxonomies and all their terms

WordPress Trac wp-trac at lists.automattic.com
Wed Mar 17 22:02:58 UTC 2010


#12629: function to remove custom taxonomies and all their terms
-----------------------------+----------------------------------------------
 Reporter:  sillybean        |       Owner:  filosofo  
     Type:  feature request  |      Status:  new       
 Priority:  normal           |   Milestone:  Unassigned
Component:  Taxonomy         |     Version:            
 Severity:  normal           |    Keywords:            
-----------------------------+----------------------------------------------
 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.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/12629>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list