Confirmation before deleting plugin options via uninstall.php

You could use a JavaScript alert to do what you’re asking, but the only way I can think of doing it would likely be a fair chunk of unnecessary added complexity.

I’ve written a small PHP script using the code from your example. If you have any difficulty amending it for your purposes, please don’t hesitate to let me know.

<?php 
    // die if not uninstalling
    if( !defined( 'WP_UNINSTALL_PLUGIN' ) )
        exit ();

    // if the "act" variable hasn't been set, display a form
    if (!isset($_GET["act"])) {
?>
    <p>Would you like to keep the options configured by this plugin?</p>
    <form action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
        <select name="act">
            <option>Select choice..</option>
            <option value="keep">Keep options</option>
            <option value="delete">Delete options</option>
        </select>
        <input type="submit" value="Go" />
    </form>
<?php
    } else {
        // if the "act" variable has been set, see if the user wants to delete the options..
        if ($_GET["act"] == "delete") {
            delete_option( 'my_options' );
            echo "Options deleted; uninstallation successful.";
            return;
        } else {
            // .. or keep them
            echo "Options kept; uninstallation successful.";
            return;
        }
    }
?>

** EDIT: PREFERENCE ROUTE **

OK, according to this post, apparently you shouldn’t / can’t use much more PHP in uninstall.php than the basic deleting of options etc.

As such, my advice would be this: create an option in your plugin settings which says “keep settings on deletion” or similar (called “DELETE_OPTIONS” in my example). Then use the following code in your uninstall.php:

<?php 
    $options = get_option('MY_PLUGIN_OPTIONS');

    if ( true === $options['DELETE_OPTIONS'] ) {
        delete_option('MY_PLUGIN_OPTIONS');
    }
?>

Regards,
Duncan

Leave a Comment