admin管理员组

文章数量:1317909

When my plugin is deactivated, I want to get a confirmation from the user whether all plugin options/tables need to be deleted or left as it is. Based on the option selected, I want to proceed further. Is it possible ? If yes, how ?

When my plugin is deactivated, I want to get a confirmation from the user whether all plugin options/tables need to be deleted or left as it is. Based on the option selected, I want to proceed further. Is it possible ? If yes, how ?

Share Improve this question asked Sep 5, 2013 at 13:37 Navin NagpalNavin Nagpal 1871 gold badge1 silver badge9 bronze badges 3
  • 2 Yes it is possible but don't confuse deactivation for uninstall. They are two very different things. You actually don't want to delete anything when a plugin is deactivated. When they uninstall is when you want to ask the user if they want to delete settings. Look into the register_uninstall_hook – user23654 Commented Sep 5, 2013 at 13:54
  • There is a discussion here which doesn't directly answer your question but will certainly help with your question: wordpress.stackexchange/questions/25910/… – Matt.C Commented Sep 5, 2013 at 13:58
  • @Matt.C Yes, I have gone thru the link but did not serve my purpose. Thanx though :) – Navin Nagpal Commented Sep 5, 2013 at 14:26
Add a comment  | 

2 Answers 2

Reset to default 1

I have implemented a workaround with JavaScript. I'm loading my own JavaScript file:

wp_enqueue_script('wp-deactivation-message', plugins_url('js/message.js', dirname(__FILE__)), array());

which contains a script that adds following Event Listener - when user clicks on our plugin deactivate button it prevent it from performing this action and display a JavaScript popup asking what you need, if user accepts it will be deactivated, if user press 'cancel' nothing happens:

message.js

window.onload = function(){
        document.querySelector('[data-slug="plugin-name-here"] a').addEventListener('click', function(event){
            event.preventDefault()
            var urlRedirect = document.querySelector('[data-slug="plugin-name-here"] a').getAttribute('href');
            if (confirm('Are you sure you want to save this thing into the database?')) {
                window.location.href = urlRedirect;
            } else {
                console.log('Ohhh, you are so sweet!')
            }
        })
}

I had trouble with the dirname(__FILE__) for some reason, so I changed my code to this, where bkj-functions is my plugin slug.

wp_enqueue_script('wp-deactivation-message', plugins_url('js/message-deactivate.js', __FILE__), array());

with the following contents for js/message-dactivate.js (since I only need an "Alert" and not a "Confirm"):

window.onload = function(){
    document.querySelector('[data-slug="bkj-functions"] .deactivate a').addEventListener('click', function(event){
        //event.preventDefault()
        alert("Reminder: If you are using the 'class' taxonomy for styling, that may disappear when this plugin has been deactivated.")
    })
}

本文标签: Show a confirmation message on plugin deactivation