0

The process to uninstall an plugin is documented here: https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/ . The sample code there is:

// if uninstall.php is not called by WordPress, die
if (!defined('WP_UNINSTALL_PLUGIN')) {
    die;
}

$option_name = 'wporg_option';

delete_option($option_name);

// for site options in Multisite
delete_site_option($option_name);

But I can find no code samples on how to ask the user if they want to remove the plugin's options from the wp-options table.

If I use the uninstall.php process to delete my plugin, is there an accepted/best practices way to ask the user if they want to also delete the plugin's options? There may be an instance where the user might want to reinstall later, and not have to re-do all of the options.

0

There are various opinions on this, but generally, if you're "deleting" (as opposed to simply "deactivating"), you don't necessarily need to confirm it. However, you certainly could.

The general approach to that is to include a setting to remove all settings upon delete or not. Let's say it's a checkbox in the plugin's settings to "delete all" and if it's not checked, you wont' clear the options. (Also, in the example, assuming that all settings are a single array, with the settings as keys - but this is just an example)

// if uninstall.php is not called by WordPress, die
if (!defined('WP_UNINSTALL_PLUGIN')) {
    die;
}

$option_name = 'wporg_option';

// $option_name['delete_all'] contains my option setting to delete all settings.
if ( 1 == $option_name['delete_all'] ) {

     delete_option($option_name);

     // for site options in Multisite
     delete_site_option($option_name);

}
  • The intent is to have a 'do you want to delete/keep the options' message when you have already deactivated the plugin and click the 'delete' link on the Plugins screen. If the plugin has already been deactivated, then your method wouldn't work, as it appears on the plugin's settings screen, I believe. – Rick Hellewell Nov 21 '19 at 21:55
  • To clarify, my option is to include it as a plugin setting (yes, on the settings screen). So the user has to have "pre-confirmed" to remove all settings, but that's the only way to do it. You can't "hook in" to the delete process for a confirmation - at least as far as I am aware. – butlerblog Nov 22 '19 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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