Example Admin Notices

Admin Notices in WordPress

Posted

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'm a WordPress developer based in Austin, Texas. Follow my projects on GitHub, or more general WordPress ramblings as @devinsays on twitter.

16 thoughts on “Admin Notices in WordPress

  1. The Frosty wrote:

    I like it. I’ve always used a site option, but if it was installed with multiple authors…

    • Devin wrote:

      Yes, there’s definitely multiple ways to do it. A standard option might be better in some cases and also make it easier to delete those settings if the plugin is deleted.

  2. Jonny wrote:

    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!

  3. Piet wrote:

    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

    • 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'))));
      [...]

      • Devin wrote:

        I verified this is an issue, but wasn’t quite able to get it working using your code Frédéric. I’ll circle back and try it again when I have a little more time.

      • 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.

      • 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.

  4. victor wrote:

    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

    • Devin wrote:

      if ( ( $pagenow == ‘plugins.php’ ) || ($pagenow == ‘plugin-editor.php’ ) )

      • victor wrote:

        thanks so much =)

  5. Gregg wrote:

    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.

  6. Stefan wrote:

    I just want to hide all these plugin notices, is there any way to do that?

  7. Hi,
    is there a default CSS class for success condition? I mean like a green one, not red or yellow.

    Thank you.

    • Devin wrote:

      I don’t believe so.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>