1

I am developing a little plugin that has couple options. For that i use this code:

add_action('admin_init', array($this, 'pa_register_settings'));
function pa_register_settings()
{
    register_setting('pa_options', 'pa_settings');
    register_setting('pa_options', 'pa_metrics');
    register_setting('pa_options', 'pa_token');
}

I would like to know what is the proper way to remove those settings from database when plugin is uninstalled?

1 Answer 1

2

WordPress has a function called register_deactivation_hook which registers a plugin function to be run when the plugin is deactivated, so:

register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
function myplugin_deactivate(){
   //delete plugins option here ex:
   delete_option('pa_options');
}

Update: Like One Trick Pony mentioned, sometimes users deactivate plugins and when they reactivate it they will loose all settings if you use this method.

So a better one would be to create an uninstall option in your plugin's admin panel and submitted, only then you should remove the options from the database and deactivate the plugin using deactivate_plugins( '/plugin-folder/plugin-name.php' );

6
  • 1
    But the user may want to re-activate the plugin later and not loose his settings Apr 13, 2012 at 16:18
  • @OneTrickPony, I feel the same way if you ask me but that's the question in hand.
    – Bainternet
    Apr 13, 2012 at 16:19
  • There's a register_uninstall_hook available too. Not sure when does this get to run, as I don't see any "uninstall" links in the plugin listing Apr 13, 2012 at 16:21
  • So rather than deleting the options at deactivation give the user the option to uninstall and delete options and deactivate at that point. Apr 13, 2012 at 16:22
  • Here we go: wordpress.stackexchange.com/questions/24600/… Apr 13, 2012 at 16:23

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.