admin_notices after register_uninstall / deactivate_hook

I’ve successfully added admin notices to display on register_activation_hook, and when the plugin updates, but I’m not seeing any way to display admin_notices after the uninstall or deactivation hooks. Is this at all possible? Alternatively can use javascript at this stage? (yes sort of).


EDIT:

To answer @G. M. I’ve already read the docs and @kaiser I’ve already seen your great writeup on these hooks, but unless I’ve been sloppy and missed something these two hooks seem to operate differently then the activation hook. Here is what I’ve tested:

This does not display a notice on the admin page, on deactivation or uninstall
(though with deactivation only , you will see the message displayed briefly on a white screen after the hook runs, but just before the plugin admin page reloads):

register_deactivation_hook(__FILE__, "gfahp_deactivate");
function gfahp_deactivate(){
    $notices = sprintf('%sSome notice%s', '<div class="error fade"><p>', '</p></div>');
echo $notices;
 }

This also does not work (on either):

register_deactivation_hook(__FILE__, "gfahp_deactivate");

function gfahp_deactivate(){  
    add_action( 'admin_notices', 'gfahp_checks', 0 );
 }
function gfahp_checks(){
    $notices = sprintf('%sSome notice%s', '<div class="error fade"><p>', '</p></div>');
    echo = $notices; // return does not work either
 }

Javascript DOES work on deactivation but NOT uninstall:

register_deactivation_hook(__FILE__, "gfahp_deactivate");
function gfahp_deactivate(){
    $notices="Some notice";
    echo "<script>window.alert('$notices');</script>";
 }

1 Answer
1

The short answer is no, it is not possible. When you deactivate your plugin, it’s deactivated so doesn’t run your admin_notices action when the admin screen is refreshed. You can confirm it for yourself by using the action in a slightly different way.

In your deactivation function include the following line:

set_transient('my_deactivation_transient', 'My plugin is being deactivated', 100);

Make you admin_notices function look like this:

function my_admin_notices()
{
    $message = get_transient('my_deactivation_transient');

    if (empty($message)) return;

    echo "<div class="error"><p>$message</p></div>";
}

When you deactivate your plugin you will not see any message. When you activate your plugin again, you will see the message. It’s not that the admin_notices function is not called, it’s just that the plugin is not active when the admin screen is refreshed.

Leave a Comment