如何清理用wordpress的switch主题创建的自定义表


How to clean custom table created with the switch theme of wordpress?

我正在wordpress中创建一个自定义表,同时在管理面板中将主题从a切换到B。

add_action('after_switch_theme', 'manage_student_reports');
function manage_student_reports() {
    $sql = "CREATE TABLE IF NOT EXISTS `wp_student_reports` (
     `report_id` int(20) NOT NULL AUTO_INCREMENT,
     `student_name` varchar(50) NOT NULL,
     `department` varchar(50) DEFAULT NULL,
     `percentage` tinyint(4) DEFAULT '0',
     PRIMARY KEY (`report_id`)
    );";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

但当我将主题从B切换回A(或其他什么)时,该表仍然存在于wordpress数据库中。有办法把它放下吗?

  1. dbDelta()函数不删除字段或表,只添加和更新
  2. 当前主题激活时,after_switch_theme挂钩运行一次。要在主题停用时执行操作,请使用switch_theme挂钩
  3. switch_theme中,钩子调用一个函数,该函数将触发检查表是否存在(为了安全并避免可能的错误),然后运行自定义DROP TABLE sql