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'm trying to drop a database table when the user uninstalls the plugin. But what does uninstalling a WordPress plugin really mean?

There's the deactivation hook and there's also the uninstall hook. And then there's the uninstall.php file.

On the constructor of the plugin class I have the uninstall hook:

register_uninstall_hook(__FILE__, array($this, 'uninstall_housekeeping'));

Then the uninstall_housekeeping method has the following code:

public function uninstall_housekeeping(){

global $wpdb;
$tweets_table = $wpdb->prefix . 'zam_tweets';

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta("DROP TABLE $tweets_table");
}

According to the wordpress codex you need to have an uninstall.php file which contains the code that will execute when the plugin is uninstalled so I also put this code:

if (!defined('WP_UNINSTALL_PLUGIN'))
exit();
global $wpdb;
$tweets_table = $wpdb->prefix . 'zam_tweets';

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta("DROP TABLE $tweets_table");

But all I can see on the WordPress plugin page is the activate and delete links when the plugin is currently deactivated. And then the deactivate and edit link if its currently active. Where's uninstall? I already tried the delete link but it doesn't seem like its doing anything except to delete the whole plugin folder, the database table is still intact.

share|improve this question
    
What is that dbDelta? Why not use $wpdb? And that require_once is kind of strange to be there... What if you try a delete_option just to confirm uninstall.php is working ok. Well, it does work ok, the problem is in your code. [edit] The first link here in the Related sidebar explains that dbDelta and the correct use of the drop table. –  brasofilo May 26 '13 at 4:26

1 Answer 1

up vote 1 down vote accepted

As explained in my comment in the Question, the error is certainly in how the DROP TABLE is being performed. But answering to How does uninstalling WordPress plugins work?:

If we do a register_uninstall_hook in our plugin, the callback is stored in the option uninstall_plugins in /wp-includes/plugin.php.

$uninstallable_plugins[plugin_basename($file)] = $callback;
update_option('uninstall_plugins', $uninstallable_plugins);

In the PHPDoc for the function register_uninstall_hook we have this:

This file (uninstall.php) will be called, if it exists, during the uninstall process bypassing the uninstall hook.

And the bypass happens in /wp-admin/includes/plugin.php:

define('WP_UNINSTALL_PLUGIN', $file);
include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
return true;

So, whatever you have in the uninstall hook or file, it should work by its own. If it does, it will work when the uninstall happens.

share|improve this answer

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.