我在 wordpress 中创建了一个插件,当插件被激活时,以下代码在 db 中创建了 2 个表。如何在停用插件时删除这两个表?register_deactivation 挂钩只删除了第一个表,第二个表在 db 中。我需要在停用插件时删除这两个表,如何编写代码
function wnm_install() {
global $wpdb, $wnm_db_version;
$sql = array();
//sms table
$sms_table = $wpdb->prefix . "smsfactory";
if( $wpdb->get_var("show tables like '". $sms_table . "'") !== $sms_table ) {
$sql[] = "CREATE TABLE ". $sms_table . " (
SfID int(11) NOT NULL AUTO_INCREMENT,
sf_name varchar(128) NOT NULL,
start_duration date NOT NULL,
end_duration date NOT NULL,
activity varchar(500) NOT NULL,
survey_settings varchar(50) NOT NULL,
`limit` varchar(50) NOT NULL,
goal varchar(100) DEFAULT NULL,
PRIMARY KEY (SfID)
) ";
}
//sms messages table
$sms_message_table = $wpdb->prefix . "smsfactorymessagetemplate";
if( $wpdb->get_var("show tables like '". $sms_message_table . "'") !== $sms_message_table ) {
$sql[] = "CREATE TABLE ". $sms_message_table . " (
sfID int(11) NOT NULL AUTO_INCREMENT,
sftemplate_name varchar(256) NOT NULL,
sftemplate_type varchar(128) NOT NULL,
PRIMARY KEY (sfID)
) ";
}
if ( !empty($sql) ) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("wnm_db_version", $wnm_db_version);
}
}
register_deactivation_hook(__FILE__, 'on_deactivate' );
function on_deactivate() {
global $wpdb; $sql = "DROP TABLE IF EXISTS techies_datachecker"; $wpdb->query($sql);
delete_option("my_plugin_db_version");
}
隔江千里
芜湖不芜
倚天杖