Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Drop Tables on Uninstall using WordPress?

On plugin activation, I have 4 tables that are created in the WordPress database.

On uninstall I'd like to have the tables deleted.

I've been able to write it out to delete the tables on deactivation. But from what I've read, it's better to keep the database tables information until the admin uninstalls rather than deactivates.

I've been looking for answers but all I seem to able to find is dropping them on deactivation. I do also have the version option I need to uninstall with it.

like image 498
jameslcarton Avatar asked Jan 06 '14 06:01

jameslcarton


2 Answers

there are two ways to do this operation on plugin uninstall/delete.

the best way i learned is by using uninstall.php

uninstall.php only loads when a plugin is removed or deleted from the plugins you can put any code in it to run when user deletes or uninstalls a plugin.

<?php
/* 
 * Removing Plugin data using uninstall.php
 * the below function clears the database table on uninstall
 * only loads this file when uninstalling a plugin.
 */

/* 
 * exit uninstall if not called by WP
 */
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit();
}

/* 
 * Making WPDB as global
 * to access database information.
 */
global $wpdb;

/* 
 * @var $table_name 
 * name of table to be dropped
 * prefixed with $wpdb->prefix from the database
 */
$table_name = $wpdb->prefix . 'table_name_to_be_dropped';

// drop the table from the database.
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );

change the "table_name_to_be_dropped" by your table name.

like image 98
Aamer Shahzad Avatar answered Oct 23 '22 12:10

Aamer Shahzad


You should not make uninstall.php as bhuthecoder said. You can name the file like you want or you can do that in single file without separate file for uninstallation.

register_uninstall_hook('uninstall.php', 'on_uninstall');

It means that WP will run uninstall.php and function on_uninstall in the uninstall.php when the plugin will be deleted. So, you can rename the file if you want, you can rename the function.

register_uninstall_hook(__FILE__, 'on_uninstall');

It means the same, but __FILE__ indicate current file which is working now and function on_uninstall should be in this file.

__FILE__ php.net

The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.

Simple example of functions for activation/deactivation/uninstallation.

function on_activation()
{
    //Some stuff
}

function on_deactivation()
{
    //Some stuff
}

function on_uninstall()
{
    //Some stuff
}

register_activation_hook(__FILE__, 'on_activation');
register_deactivation_hook(__FILE__, 'on_deactivation');
register_uninstall_hook(__FILE__, 'on_uninstall');

If you added some options you can delete it when uninstall like this:

delete_option( 'some name' );

As bhuthecoder said, you should not drop tables when deactivation, it is redundant and not friendly for a user, who can lost his data after deactivation.

There are syntax mistake in your code:

$sql = 'DROP TABLE IF EXISTS $table_name;';

Should be like this:

$sql = "DROP TABLE IF EXISTS $table_name";

If you want to put some variable in the string you should use double quotes.

And require_once is redundant. Better like this:

function e34s_db_clients_uninstall()
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'e34s_clients';
    $sql = "DROP TABLE IF EXISTS $table_name";
    $wpdb->query($sql);
    delete_option('e34s_time_card_version');
}

register_uninstall_hook(__FILE__, 'e34s_db_clients_uninstall');

Or like this:

register_uninstall_hook('uninstall.php', 'e34s_db_clients_uninstall');

With function e34s_db_clients_uninstall in the uninstall.php.

like image 15
madlopt Avatar answered Oct 23 '22 14:10

madlopt