admin管理员组

文章数量:1410737

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

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

Share Improve this question edited Dec 4, 2019 at 23:45 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Dec 4, 2019 at 21:15 LΞИIИLΞИIИ 1947 bronze badges 1
  • 1 you can show a message with that : codex.wordpress/Plugin_API/Action_Reference/admin_notices – Kaperto Commented Dec 4, 2019 at 21:33
Add a comment  | 

1 Answer 1

Reset to default 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!

本文标签: Execute Hook on the footer or header after activating a plugin