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.
dbDelta
? Why not use$wpdb
? And thatrequire_once
is kind of strange to be there... What if you try adelete_option
just to confirmuninstall.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 thatdbDelta
and the correct use of the drop table.