Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
//
?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
+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

share|improve this answer
    
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 –  drtanz Jan 10 '12 at 10:28
    
Hi drtanz. I've updated my post. –  dunc Jan 10 '12 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 Popescu Jan 10 '12 at 14:34
    
Thanks seems like a nice solution, will use that. –  drtanz Jan 10 '12 at 20:35
    
Aye it's not as good as a JavaScript alert, but it'll do the job :) Thanks for accept. –  dunc Jan 10 '12 at 20:39
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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