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.

Right now, I have this code:

function mr_np_activate(){
    // hook uninstall
    if ( function_exists('register_uninstall_hook') )
        register_uninstall_hook(__FILE__,'mr_np_uninstall');        
}
register_activation_hook(__FILE__,'mr_np_activate');


/**
 * Delete options
 *
 **/    


function mr_np_uninstall() {
    delete_option('my_plugins_options'); 
}

But when I remove my plugin, all my options are there. (I made another plugin just to show my options).

How can I delete options when plugin is removed?

[]'s

Homem Robô

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You could always use an uninstall.php file for the plugin instead.

http://codex.wordpress.org/Function_Reference/register_uninstall_hook

If the plugin can not be written without running code within the plugin, then the plugin should create a file named 'uninstall.php' in the base plugin folder. This file will be called, if it exists, during the uninstall process bypassing the uninstall hook.

When using 'uninstall.php' the plugin should always check for the WP_UNINSTALL_PLUGIN constant, before executing. The WP_UNINSTALL_PLUGIN constant is defined by WordPress at runtime during a plugin uninstall, it will not be present if 'uninstall.php' is requested directly.

That file would only need literally one line of code if the only intention is to remove an option.

<?php delete_option( 'your-option' ); ?>

Not actually addressing your question, just offering an alternative approach for dealing with plugin deactivation.

As to the problem, i think the issue is you're trying to add the deactivation callback during the activation hook, which just seems a little backward or incorrect to me, i'd assume deactivation hooks should be registered in the same way as the activation hook, but neither nested inside the other.

share|improve this answer
add 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.