I’m not sure why this isn’t more publicly promoted, but, while looking through some of the code for WordPress multisite this morning, I discovered a really helpful Easter egg.
If you develop any plugins that add extra tables to a site, you really need to know about this. There is a filter that gets applied to the list of tables being dropped whenever a site/blog within a multisite installation is deleted.
I haven’t toyed with it at all, yet, but it looks like it could be really helpful in cleaning things up when a site is deleted. When the wpmu_delete_blog() function is fired, assuming the function is instructed to completely delete the site (uploads and tables), the list of tables is run through the `wpmu_drop_tables` filter.
Theoretically, that means that your plugin could hook into the `wpmu_drop_tables` filter to add your additional tables to the list of tables that need to be dropped. This is where it gets a tiny bit touchy, though, as your plugin most likely needs to be active on the site being deleted (or network active) in order for your plugin to actually hook into that filter, but it’s a good place to start.
Too often, I go through and purge old, obsolete sites out of my development multisite space, only to find that one or more plugins have cluttered up my database with anywhere from one to 15 (yes, there are a handful of plugins that add as many as 15 tables to each site on which they’re active – needless to say, I don’t use those plugins anymore) extra tables for some plugin that was used at one point in time on the site. Hopefully, if more plugin developers are made aware of the `wpmu_drop_tables` filter, we can start to get some sort of control over that.
Again, I haven’t played with it at all, but, based on the code I saw in ms.php, it seems like the following code snippet should give you a good place to start testing this concept.
Have You Used This?
If you do have a plugin you’re developing that adds new tables to the WordPress database, and you get a chance to play around with this filter, please let us know how things go. Likewise, if you’re a plugin developer that’s already using this filter to clean up your plugin’s additional tables (or you’ve found another way to accomplish this), we’d love to hear from you.
Again, as a huge warning, I haven’t used this filter at all yet (I haven’t developed any plugins that add new tables for each individual site, so I would only be able to test it on other people’s plugins), so please, if you’re going to use this, make sure you test it thoroughly before unleashing it on your users.