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 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?

share|improve this question

1 Answer 1

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' );

share|improve this answer
1  
But the user may want to re-activate the plugin later and not loose his settings –  onetrickpony Apr 13 '12 at 16:18
    
@OneTrickPony, I feel the same way if you ask me but that's the question in hand. –  Bainternet Apr 13 '12 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 –  onetrickpony Apr 13 '12 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. –  developdaly Apr 13 '12 at 16:22
    
Here we go: wordpress.stackexchange.com/questions/24600/… –  developdaly Apr 13 '12 at 16:23

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.