Admin Notices in WordPress

Occasionally a plugin or theme will need to display a notice to users in the WordPress dashboard. This is fairly easy to do using the admin_notices hook, which shows a standard message box at the top of the screen.

Example Admin Notices

Display a Standard Notice

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

Since this div is classed “updated” the notice will display yellow. If the class is changed to “error” it displays in red.

How To Make a Dismissible Notice

With a little more work it’s also possible to display a notice stays present until the user clicks to ignore it. This could be a good way to ensure the user sees the message but also won’t get annoyed by it.

The following example was adapted from the AddThis plugin. I also use something similar in the Options Framework.

If a user clicks to hide the notice, it will save their preference in the user meta.

/* Display a notice that can be dismissed */

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);
	}
}

Display Notices Only On Certain Admin Pages

If possible, target the notice to only appear on certain pages where the user needs to see them. You can do that by using the $pagenow global variable.

For example, this notice will only appear on the plugins page:

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');

Check User Role Before Displaying a Notice

Notices should only be displayed to users that can actually do something about them. For instance, if the user cannot edit theme options, there’s no point in displaying a notice about it.

Here’s some common roles checks you might want to do:

if ( current_user_can( 'install_plugins' ) )
if ( current_user_can( 'manage_options' ) )
if ( current_user_can( 'edit_theme_options' ) )

Notice Etiquette

Notices can be annoying, so be careful with how you use them. Keep the text short and try not display more than one. Use sparingly.

Other Resources

About Devin

I am a developer based in Austin, Texas. I run a little theme shop called DevPress and help manage a WooCommerce shop with Universal Yums. Find me on twitter @devinsays.

25 Responses

  1. Jonny

    Nice post!! I have one question…

    The link to hide the box with “href=%1$s” is not working in plugin pages with “admin.php?page=”.

    Do you know a easy way to work on any page of the wp-admin?

    Thanks for this great tutorial!

  2. very informative post indeed, however I can confirm that using sub-admin pages it does not work.
    Eg. ‘themes.php’ works, but ‘themes.php?page=custom-background’ does not

    1. Indeed, the link to dismiss the notice is not taking into account the current query string.

      Here is a simple fix that should do the trick, based on Devin’s example:


      [...]
      parse_str($_SERVER['QUERY_STRING'], $params);
      printf(__('This is an annoying nag message. Why do people make these? | Hide Notice'), '?' . http_build_query(array_merge($params, array('example_nag_ignore'=>'0'))));
      [...]

      1. I found this way late, but wanted to add that Frederic’s code works beautifully. Two notes – First you must copy the html not the link which is created by the page for the “Hide Notice” area. Second, copy and paste changed the ‘=>’ to the character code for me. So that needed to be fixed.
        So the code should look like this.

        printf(__('This is an annoying nag message. Why do people make these? | Hide Notice'), '?' . http_build_query(array_merge($params, array('example_nag_ignore'=>'0'))));

        I hope that this allows the code to remain intact for anyone who finds this in the future. And thank you both Devin for sharing the original and Frederic for the fix for my issue.

      2. Seems the code tag for comments still wishes to change the code for the link. So anyone who is still stumped. Just add the “rel=’nofollow'” to the Hide Notice “a” tag in Devin’s original code behind the href.

        Sorry for needing to double post.

  3. victor

    if ( $pagenow == ‘plugins.php’ && ‘plugin-editor.php’ )
    with this metod only show in plugins.php

    if ( $pagenow == ‘plugins.php’ || ‘plugin-editor.php’ )
    and with show in all pages?? help me please

  4. Gregg

    Could this be used to add a notice to let a user know that this page is using a template? How could you conditionally pick a certain page. Example: You create a page called Testimonials and then you have a template (testimonials.php) that it uses.

  5. Luis Rock

    Hello Devin,

    would you help me?

    I am building a plugin to display a bar on a wordpress website. The plugin is almost done. What I need now is a way to show the bar on the plugin settings page, so admin can save the settings and see the bar on the same page. And I don’t know why. The url is mywebsite.com/wp-admin/options-general.php?page=master-bar .
    When you click save settings, the url becomes mywebsite.com/wp-admin/options-general.php?page=master-bar&settings-updated=true .

    Any hint. Thanks anyway!

    Regards

      1. Luis Rock

        Devin, for a newbie, everything is an issue…LOL

        I am using add_action( 'wp_head', 'masterbar_main_function' ) to display the bar. Then, on the ‘master bar_main_function’, I am building the bar and, with conditional tags, telling wordpress where to put it (homepage, or posts or pages etc).

        But when I tell wordpress to put the bar at ‘mywebsite.com/wp-admin/options-general.php?page=master-bar’, I am not getting a result. Do you know why?

  6. Thank you Devin, great post! I’m having a little issue where $_GET[‘post’] is not working, or rather it is for one load then the next load it is just not even present. Not sure if that makes sense :s

  7. Hello,

    I have using above code in my custom plugin for creating notice that working good. But i have another question in my case, in my plugin i have added 3 buttons for plugin rating like `No thanks, remind me later and rate it now` and i want when user click on `remind me later` button so plugin notice hide for 7 days and regenerate again after 7 days.

    I don’t have any idea how to do this. Can you help me out about this?

    Thanks for your reply.
    Divyesh Kakrecha

Leave a Reply to Luis Rock Cancel reply