15

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?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Thompson
  • 1,954
  • 10
  • 33
  • 58

6 Answers6

29

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.

<?php
/*
Plugin Name: My Plugin
*/

register_activation_hook(__FILE__, 'my_plugin_activation');
function my_plugin_activation() {
  $notices= get_option('my_plugin_deferred_admin_notices', array());
  $notices[]= "My Plugin: Custom Activation Message";
  update_option('my_plugin_deferred_admin_notices', $notices);
}

add_action('admin_init', 'my_plugin_admin_init');
function my_plugin_admin_init() {
  $current_version = 1;
  $version= get_option('my_plugin_version');
  if ($version != $current_version) {
    // Do whatever upgrades needed here.
    update_option('my_plugin_version', $current_version);
    $notices= get_option('my_plugin_deferred_admin_notices', array());
    $notices[]= "My Plugin: Upgraded version $version to $current_version.";
    update_option('my_plugin_deferred_admin_notices', $notices);
  }
}

add_action('admin_notices', 'my_plugin_admin_notices');
function my_plugin_admin_notices() {
  if ($notices= get_option('my_plugin_deferred_admin_notices')) {
    foreach ($notices as $notice) {
      echo "<div class='updated'><p>$notice</p></div>";
    }
    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

Community
  • 1
  • 1
kitchin
  • 774
  • 6
  • 9
  • This looks good, my only comment would be to DRY up the part where you add a new notice. – pguardiario Sep 27 '14 at 07:52
  • Yep, the three lines `$notices= get_option(...); $notices[]=...; update_option(..., $notices)` can be abstracted out to general purpose `my_plugin_add_notice()` function. You often see this with a parameter for 'note' vs. 'error'. Then the then rendering function displays it in the WP fashion as a blue or red banner, using css class 'update' or 'error', if I recall correctly. – kitchin Sep 27 '14 at 18:40
  • Superb solution. Thanks – Alaksandar Jesus Gene Oct 05 '20 at 12:31
  • This works but you have to run the admin_notices for every admin page. Is there no way to just add to the wordpress notices. seems like every plugin would have to implement this kind of functionality. – Leo Jul 06 '23 at 13:43
8

This is so simple to show a notice

function your_admin_notice(){
echo '<div class="updated">
   <p>I am a little yellow notice.</p>    
</div>';    
}
add_action('admin_notices', 'your_admin_notice');

But if you want to show a Dismissible Notice then try below

    add_action('admin_notices', 'example_admin_notice');

function example_admin_notice() {
    global $current_user ;
        $user_id = $current_user->ID;
        /* Check that the user hasn't already clicked to ignore the message */
    if ( ! get_user_meta($user_id, 'example_ignore_notice') ) {
        echo '<div class="updated"><p>'; 
        printf(__('This is an annoying nag message.  Why do people make these? | <a href="%1$s">Hide Notice</a>'), '?example_nag_ignore=0');
        echo "</p></div>";
    }
}

add_action('admin_init', 'example_nag_ignore');

function example_nag_ignore() {
    global $current_user;
        $user_id = $current_user->ID;
        /* If user clicks to ignore the notice, add that to their user meta */
        if ( isset($_GET['example_nag_ignore']) && '0' == $_GET['example_nag_ignore'] ) {
             add_user_meta($user_id, 'example_ignore_notice', 'true', true);
    }
}

And if you want to show that notice on certain page try below condition.

    function my_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'plugins.php' ) {
         echo '<div class="updated">
             <p>This notice only appears on the plugins page.</p>
         </div>';
    }
}
add_action('admin_notices', 'my_admin_notice');

You can see here

Ibnul Hasan
  • 431
  • 5
  • 13
  • Am I seeing this correctly that I need to create a call back function for every message I want to display? How would I create a function that takes a parameter that specifies what the error message is? – chizou Oct 21 '15 at 05:29
  • Ok, if you want to display error message then there are other way actually. For displaying admin_notice with parameter you could try the top most answer here. Also you could find a way from the links below http://stackoverflow.com/questions/1242328/wordpress-displaying-an-error-message-hook-admin-notices-fails-on-wp-insert-p – Ibnul Hasan Oct 23 '15 at 16:04
3

Just use a <div class='updated'>. For example -

echo "<div class='updated'>Test Plugin Notice</div>";
ronakg
  • 4,038
  • 21
  • 46
  • yes, but then this notice is showing all the times, I want like this notice will disappear, when i click on config link in that notice (after activating) – Thompson Mar 21 '12 at 15:00
  • 1
    In that case you just have to add a flag that will store if user visited the config of the plugin. You can store this flag in ``wp_options`` table. – ronakg Mar 21 '12 at 15:01
3

You can use the new admin notices to create what are called admin pointers using show_wp_pointer_admin_bar.

Linky: http://wpengineer.com/2272/how-to-add-and-deactivate-the-new-feature-pointer-in-wordpress-3-3/

Wyck
  • 2,023
  • 4
  • 28
  • 34
1

The proper way to add your notices is to echo it in your hook for admin_notices action:

function wpse8170_admin_notice(){
    echo '<div class="updated"><p>This is my notice.</p></div>';
}
add_action('admin_notices', 'wpse8170_admin_notice');
Eugene Manuilov
  • 4,271
  • 8
  • 32
  • 48
0

I've developed amarkal-admin-notification - a script that lets you add static/dismissible admin notices and handles the dismissal for you. This script is a module within the Amarkal WordPress framework.

For example:

amarkal_admin_notification( 'my-error-notice', __('Oh snap! This is an <strong>error</strong> message.','slug'), 'error');
Yoav Kadosh
  • 4,807
  • 4
  • 39
  • 56