2

I'm creating my first plugin (modifying an existing one for what I need) and although it's working fine i am trying to create a simple options menu to allow either deactivation of the plugin, or deactivation and removal of tables that the plugin creates.

In the options panel I have the following form to allow an admin to click a checkbox and then submit a form which i am hoping to get to:

  1. Remove tables created by the plugin
  2. Deactivate the plugin
  3. Redirect back to the plugin page

Here is the code for my options page:

<p class="submitmetataxonomyuninstall">
<form method="post" action=""; onsubmit="if (!this.verify.checked) return false;">
<?php _e("Also Delete all Tables & Data from Database:", 'menu-test' ); ?>
    <input type="checkbox" name="verify" /></p>
<p><i>(IMPORTANT: This action CANNOT be reversed and will remove all tables and data from the database.)</i></p>
<p class="submit"><input type="submit" name="submitmetataxonomyuninstall" class="button-primary" value="<?php esc_attr_e('Deactivate & Uninstall') ?>" /><small> (Checkbox above must be checked for button to work.)</small></p>
</form>
</p>

I just can't get this to work however.

This would be the function I think I need to call (when this is in the main plugin function it deactivates and removes the tables just fine):

register_deactivation_hook( __FILE__, 'simple_term_meta_uninstall' );
function simple_term_meta_uninstall() {
    global $wpdb;

    if (function_exists('is_multisite') && is_multisite()) {
        // check if it is a network activation - if so, run the activation function for each blog id
        if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
            $old_blog = $wpdb->blogid;
            // Get all blog ids
            $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
            foreach ($blogids as $blog_id) {
                switch_to_blog($blog_id);
                _simple_term_meta_uninstall();
            }
            switch_to_blog($old_blog);
            return;
        }   
    } 
    _simple_term_meta_uninstall();      
}

I believe this also calls this function from within the existing plugin file:

register_uninstall_hook( __FILE__, 'simple_term_meta_uninstall' );

function _simple_term_meta_uninstall()
{
    global $wpdb;

    $table_name = $wpdb->prefix . 'termmeta';

    $wpdb->query("DROP TABLE IF EXISTS $table_name");

    delete_option( "simple_term_meta_db_version" );
}

How would I call this function using the form shown?

1 Answer 1

1

You just need to obtain the basename of any plugin to deactivate it.

if( $uninstall == true ) {
    delete_option( 'CHANGETHIS' );
    deactivate_plugins( plugin_basename( __FILE__ ) );
}

Generally they look like mypluginfolder/mypluginmasterfile.php

Give that a shot.


There are a variety of ways to getting it to work.

If you want a button on the options page to call that, you could just POST to the options page and check to see if the uninstall button was clicked.

Something like:

$uninstall = isset( $_POST[ 'uninstall' ] ) ? true : false;
2
  • Thanks Grok - How do I link the function to uninstall to the submit button in the options menu i've created though?
    – Rob
    Nov 9, 2011 at 11:19
  • I've updated the answer to reflect a way to accomplish that! Best of luck. Nov 10, 2011 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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