1

I have the following code fraction, but it does not work, I want that after activating a plugin this calls an action on the footer and prints something, but I can't do it, the problem seems that the admin_footer does not run, I don't know if activated_plugin is just before or after the html is printed so that the admin_footer, or how could I fix it?

add_action( 'activated_plugin', 'admin_activated_plugin' );
function admin_activated_plugin(){
  // up to here if you run ....
  // but
  // the hook below doesn't
  add_action( 'admin_footer', 'after_activate_print_footer', 99 );
}
function after_activate_print_footer(){
 echo "something";
}

In conclusion, I want to print a text in the header or footer just after a plugin is activated

1

1 Answer 1

2

Checking the action hooks API reference, "activated_plugin" is an Advanced hook. It operates outside the normal loop which would permit you to do what you want.

A better way would be to add_action('admin_footer_text'), as illustrated in this WPBeginner article.

However, if you only want this to show when a particular plugin is activated, you can add the following "if" statement to check if your plugin is active:

if (is_plugin_active($plugin_path)){
    add_filter('admin_footer_text', '[your function here]');
}

If you only want this to run once, immediately after the plugin is activated, you might use your "activated_plugin" to set an option variable. Then, use your "admin_footer_text" action to check for and clear that option variable:

// Set the option indicating the plugin was just activated
function wpse_set_activated(){
    add_option("wpse_my_plugin_activated",'true');
}
add_action("activated_plugin","wpse_set_activated");

// Setup conditional admin_footer text mod
function wpse_setup_admin_footer_text(){
    if (is_plugin_active("plugin_folder/my_plugin.php")){
        $just_activated = get_option("wpse_my_plugin_activated",'false');
        if ($just_activated !== 'true') {
            echo "";
        } else {
            echo "My plugin is active";
            delete_option("wpse_my_plugin_activated");
        } // end if is_active
    } else {
        echo "";
    }// end if is_plugin_active
}
add_filter('admin_footer_text', 'wpse_set_admin_footer_text');

Hope this helps!

1
  • It is an elegant solution, add a cookie within the function "wpse_set_activated" and "wpse_setup_admin_footer_text" especially so that "wpse_setup_admin_footer_text" does not run just get_option, you must first know if the cookie is active from "wpse_set_activated" then enter and delete, just as you did but with cookie.
    – LΞИIИ
    Commented Dec 5, 2019 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.