I created a plugin and want to add a function to delete my tables from the database when a user deletes my plugin. I created a function that deletes tables from the DB when a user deactivates my plugin, but I don't want that. Here is the code:
// Delete table when deactivate
function my_plugin_remove_database() {
global $wpdb;
$table_name = "NestoNovo";
$sql = "DROP TABLE IF EXISTS $table_name;";
$wpdb->query($sql);
delete_option("my_plugin_db_version");
}
register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );
As you can see, this function deletes tables when the plugin is deactivated, but I need to do that when the plugin is deleted.
I created a plugin and want to add a function to delete my tables from the database when a user deletes my plugin. I created a function that deletes tables from the DB when a user deactivates my plugin, but I don't want that. Here is the code:
// Delete table when deactivate
function my_plugin_remove_database() {
global $wpdb;
$table_name = "NestoNovo";
$sql = "DROP TABLE IF EXISTS $table_name;";
$wpdb->query($sql);
delete_option("my_plugin_db_version");
}
register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );
As you can see, this function deletes tables when the plugin is deactivated, but I need to do that when the plugin is deleted.
Share Improve this question edited Nov 22, 2014 at 5:08 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Nov 21, 2014 at 21:38 x-y-z-selectx-y-z-select 1931 gold badge1 silver badge8 bronze badges 2- Have you tried register_uninstall_hook? – Andrew Bartel Commented Nov 21, 2014 at 22:01
- Thank you for answer.. yes.. i try that.. nothing happend.. :/ – x-y-z-select Commented Nov 21, 2014 at 22:44
6 Answers
Reset to default 30You could do this using the WordPress uninstall.php support:
<?php
if( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) exit();
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS NestoNovo" );
delete_option("my_plugin_db_version");
?>
This uninstall.php file is called when your plugin is deleted.
Enter code here:
register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );
function my_plugin_remove_database() {
global $wpdb;
$table_name = $wpdb->prefix . 'NestoNovo';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
delete_option("my_plugin_db_version");
}
You need to use register_uninstall_hook
hook instead of register_deactivation_hook
to delete tables from the database.
register_deactivation_hook
fires when we deactivate a plugin and register_uninstall_hook
fires when we want to remove/delete
our plugin.
Please use this code if you have only one table:
function delete_plugin_database_table(){
global $wpdb;
$table_name = $wpdb->prefix . 'table_name';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
}
register_uninstall_hook(__FILE__, 'delete_plugin_database_table');
If you have more than two tables then you use this code:
function delete_plugin_database_tables(){
global $wpdb;
$tableArray = [
$wpdb->prefix . "table_name1",
$wpdb->prefix . "table_name2",
$wpdb->prefix . "table_name3",
$wpdb->prefix . "table_name4",
];
foreach ($tableArray as $tablename) {
$wpdb->query("DROP TABLE IF EXISTS $tablename");
}
}
register_uninstall_hook(__FILE__, 'delete_plugin_database_tables');
Reference Links:
https://developer.wordpress/reference/functions/register_uninstall_hook/ https://developer.wordpress/plugins/plugin-basics/uninstall-methods/
I know that there's this hook called: register_deactivation_hook
that you can use to do stuff when the plugin is deactivated.
Take a look at the documentation and see if it is what you're looking for.
For instance:
**register_deactivation_hook**(__FILE__, 'sm_deactivation');
function myplugin_deactivation(){
/*
Stuff
*/}
If you are using "WORDPRESS PLUGIN BOILERPLATE GENERATOR" wppb
go to includes\class-...-deactivator.php
and write the following code ( modify please as your needs)
global $wpdb;
$tableArray = [
$wpdb->prefix . "table1",
$wpdb->prefix . "table2",
];
foreach($tableArray as $table){
$wpdb->query("DROP TABLE IF EXISTS $table");
}
Thanks
Unfortunately, WordPress does not expose functionality to do that. It only supports the register_uninstall_hook hook. This hook is called when the user clicks on the uninstall link that calls for the plugin to uninstall itself. The link won't be active unless the plugin hooks into the action. see http://codex.wordpress/Function_Reference/register_uninstall_hook
and the register_deactivation_hook hook. What most plugin developer do is add an checkbox to the setting table with the use of get_option, update_option. When this option is checked, the data is removed.
This way, temporary deactivation does not reset the option table of your plugin.