5

I'm creating a WordPress plugin and have the uninstall.php file ready. It deletes the options used by the plugin on deletion. What I'd like to do is to popup a dialog box asking whether to delete or keep these options when the user decides to delete the plugin. How can I do this?

<?php
// If uninstall not called from WordPress exit
if( !defined( 'WP_UNINSTALL_PLUGIN' ) )
exit ();
// Delete option from options table
delete_option( 'my_options' );
//
?>

1 Answer 1

2
+50

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

5
  • I've tried this out but I don't get the confirmation screen. I've attached the test plugin I'm using to try it out, 2shared.com/file/LcfDix_X/test-plugin.html
    – urok93
    Jan 10, 2012 at 10:28
  • Hi drtanz. I've updated my post.
    – turbonerd
    Jan 10, 2012 at 11:39
  • The plugin settings option is the best way to go. I add a check box option to all plugins I create.
    – Ciprian
    Jan 10, 2012 at 14:34
  • Aye it's not as good as a JavaScript alert, but it'll do the job :) Thanks for accept.
    – turbonerd
    Jan 10, 2012 at 20:39
  • Just to think about this... What would be the default value of "$options['DELETE_OPTIONS']" to start with? its not user friendly at all. Un-install process should ask user to make that choice at the point and not in admin options. Jan 16, 2012 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.