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 ?
2 Answers
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!')
}
})
}