Skip to content

Instantly share code, notes, and snippets.

@felixarntz
Last active July 7, 2016 15:05
WordPress Plugin Uninstallation
<?php
// Callback function for `register_uninstall_hook()`.
function myplugin_uninstall() {
global $wpdb;
// Check if we are on a Multisite or not.
if ( is_multisite() ) {
// Retrieve all site IDs from all networks (WordPress >= 4.6 provides easy to use functions for that).
if ( function_exists( 'get_sites' ) ) {
$site_ids = get_sites( array( 'fields' => 'ids' ) );
} else {
$site_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs;" );
}
// Uninstall the plugin for all these sites.
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
myplugin_uninstall_single_site();
restore_current_blog();
}
} else {
myplugin_uninstall_single_site();
}
}
register_uninstall_hook( __FILE__, 'myplugin_uninstall' ); // __FILE__ is supposed to be the plugin's main file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment