停用插件时如何删除2个表

我在 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");

}


浮云间
浏览 124回答 3
3回答

隔江千里

您需要将表添加到数组中。试试下面的代码:function on_deactivate() {    global $wpdb;    $tables = array(        $wpdb->prefix . "smsfactory",         $wpdb->prefix . "smsfactorymessagetemplate"    );    foreach ( $tables as $table ) {        $sql = "DROP TABLE IF EXISTS $table";        $wpdb->query($sql);    }    delete_option("my_plugin_db_version");}register_deactivation_hook( __FILE__, 'on_deactivate'  );希望能帮到你。

芜湖不芜

function delete_tbl_from_db(){    global $wpdb;    $tbl_array = [         $wpdb->prefix . "first_table",      $wpdb->prefix . "second_table",  ];  foreach ($tbl_array as $tbl_name) {     $wpdb->query("DROP TABLE IF EXISTS $tbl_name");  }}register_uninstall_hook(__FILE__, 'delete_tbl_from_db');Please check above code.

倚天杖

function delete_tbl_from_db(){    global $wpdb;    $tbl_array = [        $wpdb->prefix . "smsfactory",        $wpdb->prefix . "smsfactorymessagetemplate",    ];    foreach ($tbl_array as $tbl_name) {     $wpdb->query("DROP TABLE IF EXISTS $tbl_name");    }}register_deactivation_hook( __FILE__, 'delete_tbl_from_db' );This is working on my site. Please check it
打开App,查看更多内容
随时随地看视频慕课网APP