How to remove plugin-specific custom taxonomy terms when plugin is uninstalled?

I'm trying to write a class that would allow for easy removal of all plugin-related data when the plugin is uninstalled. Specifically, I want it to remove plugin options, custom post type entries, and custom taxonomies and their terms.

What I have written mostly works, but the get_terms function returns an 'invalid_taxonomy' error. Looking into it further, I found that the global $wp_taxonomies does not recognize the custom taxonomies created by the plugin during the uninstall.php process.

So my question is: How does one remove custom taxonomy terms when a plugin is uninstalled?

Here is my code: http://pastebin.com/geH1wzRC

Bonus points if you can tell me how to avoid loading this class twice if two plugins contain it. I tried just wrapping the class in if( !class_exists( 'WP_Plugin_Janitor' ) ) { ... } but then the class never loaded.

EDIT I was able to prevent the class from loading more than once by putting the class in a separate file and wrapping the include_once statement in the if( !class_exists( statement.

Thanks in advance!

Topic terms uninstallation custom-taxonomy plugin-development Wordpress

Category Web


You should be using the register_deactivation_hook and/or register_uninstall_hooks. That may be why some functions don't work. I'd have to do some testing to be sure, but loading your method directly like WP_Plugin_Janitor::cleanup( $opt, $cpt, $tax ); seems like a pretty good way to skip over some of the WordPress load sequence or to run things out of order.

As far as loading the class twice, I have an answer, but that is a different question and not really WordPress specific. :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.