0

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!

6
  • PS: I'm surprised WP doesn't have a built-in function like this. It would give plugin developers a universal way of cleaning up after themselves... Dec 27, 2012 at 15:28
  • I think this is a rabbit hole, core.trac.wordpress.org/ticket/11058
    – Wyck
    Dec 27, 2012 at 16:01
  • Yeah...I see what you mean. It looks like there's more to removing a taxonomy than just unsetting it. That said, I still would love to know how to remove taxonomy terms during the uninstall.php process. Dec 27, 2012 at 16:16
  • Do you get a fatal error at get_terms or does it just not return anything?
    – s_ha_dum
    Dec 27, 2012 at 16:33
  • No fatal error. If I do a var_dump on $terms, I get this: object(WP_Error)#246 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data"]=> array(0) { } } object(WP_Error)#248 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data"]=> array(0) { } } Dec 27, 2012 at 16:37

1 Answer 1

1

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. :)

1
  • Thanks for the quick response! I'm actually putting this in the uninstall.php file, so it should behave the same as using register_uninstall_hook. Regarding the class loading twice question, I already resolved that...see my edit above. Thanks again! Dec 27, 2012 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.