您需要使用register_uninstall_hook
挂钩而不是register_deactivation_hook
从数据库中删除表。
register_deactivation_hook
当我们停用插件时register_uninstall_hook
触发,并在需要remove/delete
插件时触发。
如果只有一个表,请使用此代码:
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');
如果您有两个以上的表,则使用此代码:
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/