WordPress.org

Ready to get started?Download WordPress

Forums

How can I remove an option from ALL option tables in multisite? (3 posts)

  1. philipwalton
    Member
    Posted 3 years ago #

    Like any good plugin author, I'm wanting to remove the options my plugin creates when a user uninstalls it. I have the following code implemented in my uninstall.php file:

    delete_option('my_plugin_option');

    However, if my plugin was running on a multisite installation, and it was activated and/or used on multiple subsites, then the above method won't work. The above method will remove the option from the wp_options table, but it won't remove it from tables wp_2_options, wp_3_options, etc.

    I know I can just write a complete SQL script to go through all the tables and delete the options, but it seems like there needs to be a much more user-friendly way to help ensure all plugin authors are able to clean up after themselves.

    Anyone know of such a way?

  2. philipwalton
    Member
    Posted 3 years ago #

    I'd still like a better answer if anyone know one, but this is how I'm currently tackling this problem. In my uninstall.php file I have the following code:

    if (is_multisite()) {
        global $wpdb;
        $blogs = $wpdb->get_results("SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A);
        if ($blogs) {
            foreach($blogs as $blog) {
                switch_to_blog($blog['blog_id']);
                delete_option('my_plugin_option');
            }
            restore_current_blog();
        }
    } else {
        delete_option('my_plugin_option');
    }

    I've tested this, and it successfully removes all options from all tables when you delete a plugin. It also does the normal uninstall if you're not running a multisite setup.

  3. RobertHarm
    Member
    Posted 2 years ago #

    thanks! works great! it also works for removing plugin tables. I would advise you to add the following code at the beginning of uninstall to check, if uninstall.php is called from WordPress exit:

    //if uninstall not called from WordPress exit
    if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
    	exit ();

Topic Closed

This topic has been closed to new replies.

About this Topic