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.
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 optionuninstall_plugins
in/wp-includes/plugin.php
.In the PHPDoc for the function
register_uninstall_hook
we have this:And the bypass happens in
/wp-admin/includes/plugin.php
: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.