How to display notice in admin panel on Plugin Activation?

前端 未结 6 2014
我寻月下人不归
我寻月下人不归 2020-12-02 19:27

I am trying to display a notice in admin panel when I activate my test plugin.
How can I display that? What\'s that method?

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 20:01

    For plugin activations, the 'admin_notices' hook cannot be used directly, because there is a redirect. A workaround is to store your notice in the options table and check for it next time. Also, if you also want to cover plugin upgrades as well as activations, you will need to use another hook, such as 'admin_init' (since WP 3.1, see http://make.wordpress.org/core/2010/10/27/plugin-activation-hooks/).

    Here is a complete sample plugin handling both activation and upgrade. I made the deferred notice an array so you can stack them up.

    $notice

    "; } delete_option('my_plugin_deferred_admin_notices'); } } register_deactivation_hook(__FILE__, 'my_plugin_deactivation'); function my_plugin_deactivation() { delete_option('my_plugin_version'); delete_option('my_plugin_deferred_admin_notices'); }

    UPDATE: There's also a common way to use set_transient() instead of update_option(), and to direct messages to the correct admin user. This post concerns metaboxes, not plugin activation, but the techniques work the same just about everywhere in Dashboard, as far as I know: https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices

提交回复
热议问题