-
-
Save Balakrishnan-flycart/969ff5ae9b68d2e953e477cea3089a78 to your computer and use it in GitHub Desktop.
Delete Extra Plugin Tables When a Site Is Deleted in WordPress Multisite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add any custom tables that this plugin creates for an individual site to the list | |
* of tables that get deleted when a site is deleted. | |
* @see https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-admin/includes/ms.php#L116 | |
*/ | |
add_filter( 'wpmu_drop_tables', 'delete_my_plugin_tables', 10, 2 ); | |
function delete_my_plugin_tables( $tables=array(), $blog_id=null ) { | |
/** | |
* Make sure the blog ID parameter was sent, so we don't | |
* accidentally delete tables for the wrong blog | |
*/ | |
if ( empty( $blog_id ) || 1 == $blog_id || $blog_id != $GLOBALS['blog_id'] ) | |
return $tables; | |
/** | |
* Assume our plugin added three new tables called "plugin_table_1", "plugin_table_2" and "plugin_table_3" | |
* to each site on which it's active | |
*/ | |
global $wpdb; | |
$blog_prefix = $wpdb->get_blog_prefix( $blog_id ); | |
$base_prefix = $wpdb->base_prefix; | |
$plugin_tables = array( 'plugin_table_1', 'plugin_table_2', 'plugin_table_3' ); | |
/** | |
* Since the $wpdb->tables() call in the wpmu_delete_blog() function is called without the | |
* $prefix parameter, the list of tables sent through this filter will all be prefixed | |
* with the appropriate blog prefix before it gets to this filter. We need to prefix | |
* our list of tables, as well, before sending it back. | |
*/ | |
foreach ( $plugin_tables as $k => $table ) { | |
$tables[$table] = $blog_prefix . $table; | |
} | |
return $tables; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment