【问题标题】:Confirm message to delete custom table when deactivating plugin停用插件时确认删除自定义表的消息
【发布时间】:2026-02-10 08:40:01
【问题描述】:

我对 WordPress 和 php 完全陌生。我的 Wordpress 插件中有一个自定义表格,我想在停用插件时删除自定义表格(卸载时不删除)。 这很容易完成,但在删除表之前我想要求确认。

function va_deactivation() {
.... //unregisters settings

//Asks for confirmation - Code here


//If answer is yes proceed to delete. If no, doesn't execute the following code 
global $wpdb;
$pa_table = $wpdb->prefix."tableName";
$sql = 'DROP TABLE IF EXISTS '.$pa_table;
$wpdb->query( $sql );

}

有什么帮助吗?谢谢

【问题讨论】:

    标签: php mysql wordpress confirmation


    【解决方案1】:

    jQuery(function () {
        jQuery('.deactivate a').click(function (e) {
            let url = jQuery(this).attr('href');
            let regex = /[?&]([^=#]+)=([^&#]*)/g,
                    params = {},
                    match;
            while (match = regex.exec(url)) {
                params[match[1]] = match[2];
            }
            if(params.plugin === "{plugin_name}%2F{plugin_name}.php"){
                let delete_confirm = confirm("You are going deactivate this plugin");
                if (delete_confirm !== true) {
                    e.preventDefault()
                }
            }
        });
    });

    我用过这个

    但最好使用register_uninstall_hook

    【讨论】:

      最近更新 更多