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.
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.
I like it. I’ve always used a site option, but if it was installed with multiple authors…
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.
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!
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'))));
[...]
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.
This is what worked best for me:
$hide_url = add_query_arg( 'ehc_ignore_notice', '0' );
Seems pretty simple, any reason why it won’t work?
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
if ( ( $pagenow == ‘plugins.php’ ) || ($pagenow == ‘plugin-editor.php’ ) )
thanks so much =)
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.
I just want to hide all these plugin notices, is there any way to do that?
I wrote a small plugin that can be used either as a plugin or within your theme function.php file.
You can see the full tutorial here: http://kevinphillips.co.nz/news/custom-post-type-validation-without-javascript
Hi,
is there a default CSS class for success condition? I mean like a green one, not red or yellow.
Thank you.
I don’t believe so.
Thanks. Very useful.
With the new admin design, alert messages are very ‘subtle’ and easily overlooked. More pleasing but less effective.
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
Why is that an issue? It’s just adding a query string to the url.
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?
I still don’t get what you are trying to do. Admin notices can only be displayed on the backend, so I’m not sure how you could be putting it on posts or pages. Do you mean the backend edit pages?
Echo out $pagenow and navigate to the page where you want the notice added to see what the actual slug is that you need to use.
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
Great article! Thanks for sharing. I think you might be interested in a small script that I’ve created, which helps you add dismissible/static admin notifications to WordPress. Hope you’ll find it useful. You can see the project on GitHub https://github.com/askupasoftware/wp-admin-notification
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