Plugin Uninstall and Deactivate via Options Menu

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>

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) {

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

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;

Leave a Comment