Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've just realized that the traditional uninstall.php file along a plugin is not working in Multisite.

if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) 
    exit();

delete_option( 'plugin_option_name' );

This doesn't delete the sub-sites options in all wp_SITE-ID_options tables.

Is there a standard way for doing this?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Searching inside all uninstall.php files that I have in my hard-drive, I've found two that had the function is_multisite(): User Role Editor and Add Code to Head.

Both use a $wpdb loop. Simplified:

<?php
/**
 * Plugin Uninstall Procedure
 */

// Make sure that we are uninstalling
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) 
    exit();

// Leave no trail
$option_name = 'plugin_option_name';

if ( !is_multisite() ) 
{
    delete_option( $option_name );
} 
else 
{
    global $wpdb;
    $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
    $original_blog_id = get_current_blog_id();

    foreach ( $blog_ids as $blog_id ) 
    {
        switch_to_blog( $blog_id );
        delete_option( $option_name );     

        // OR
        // delete_site_option( $option_name );  
    }

    switch_to_blog( $original_blog_id );
}

Related Q&A: Uninstall, Activate, Deactivate a plugin: typical features & how-to

share|improve this answer
1  
You shold use restore_current_blog instead of storing the current blog id and switching back later –  bungeshea_ Jan 13 '13 at 4:57
    
@bungeshea, yes, much more elegant :) I was about to edit the Answer but am wondering: Multisite uninstall.php won't run always from blog ID == 1? –  brasofilo Jan 13 '13 at 5:04
    
You can only delete plugins from the network dashboard, which is on the main site (blog id 1) –  bungeshea_ Jan 13 '13 at 5:06
    
Yep, sure, and, after all, it's nonsense using switch_to_blog(1) when we have the restore_ function... Don't recall exactly where I read something about changing the main blog instance... –  brasofilo Jan 13 '13 at 5:12
    
Have you tested this thoroughly? Looking at the source code, it would seem the uninstall.php file within a plugin would only be called if 1) it existed prior to the plugins activation (if so, the plugin name would be stored within the blog's option table); 2) when calling get_option('uninstall_plugins') actually yields that specific plugin. It doesn't seem to work on Multisite because network-wide plugin info is stored in the sitemeta table, instead. –  tbuteler Feb 19 '13 at 10:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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