Uninstall Hook is not working proper as when I am uninstalling the plugin, the table are not being dropped. Please help me with this. Here is the following code I am using. Please tell me what I am missing.
register_uninstall_hook( __FILE__, 'plugin_db_uninstall' );
function plugin_db_uninstall() {
global $wpdb;
$table_name = $wpdb->prefix."user_master";
$wpdb->query("DROP TABLE IF EXISTS $table_name");
$table_name = $wpdb->prefix."candidate_master";
$wpdb->query("DROP TABLE IF EXISTS $table_name");
}
Full Code
define('TXTFOLDER', plugins_url()."/candidate_section/txtfiles");
function candidate_install () {
if (!file_exists(TXTFOLDER)) {
mkdir(TXTFOLDER, 0777);
}
}
function candidate_section_create_table()
{
global $wpdb;
$sql =
"CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."user_master`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT,NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
dbDelta($sql);
$sql1 =
"CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."candidate_master`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`position` varchar(255) NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
dbDelta($sql1);
}
function plugin_db_uninstall() {
global $wpdb;
$table_name = $wpdb->prefix."user_master";
$wpdb->query("DROP TABLE IF EXISTS $table_name");
$table_name = $wpdb->prefix."candidate_master";
$wpdb->query("DROP TABLE IF EXISTS $table_name");
}
if (is_admin()) {
add_action('admin_menu', 'candidate_menu');
register_activation_hook(__FILE__, 'candidate_install');
register_activation_hook( __FILE__, 'candidate_section_create_table' );
register_uninstall_hook( __FILE__, 'plugin_db_uninstall' );
}
I’m not positive about the cause, but I can see two things. One is that you could remove all 3
register_*_hook
from insideis_admin()
and leave them outside any conditional.PS: why do you have two
*_activation_hook
s?The other is using the file
uninstall.php
instead ofregister_uninstall_hook
. I’m not finding the link where I saw the pitfalls of the hook discussed, but since a long time I only use theuninstall.php
method.There’s this note in the Codex, but no further reference on the why is given:
And a link to this WordPress Answers: Uninstall, Activate, Deactivate a plugin: typical features & how-to.
But check all the results over there.