您需要使用register_uninstall_hook
钩子代替register_deactivation_hook
从数据库中删除表。
register_deactivation_hook
当我们停用插件并register_uninstall_hook
想开枪就开枪remove/delete
我们的插件。
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\');
参考链接:
https://developer.wordpress.org/reference/functions/register_uninstall_hook/https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/