Prints admin screen notices.
In order to display a notice, echo a div with the class notice
and one of the following classes:
* notice-error
– will display the message with a white background and a red left border.
* notice-warning
– will display the message with a white background and a yellow/orange left border.
* notice-success
– will display the message with a white background and a green left border.
* notice-info
– will display the message with a white background a blue left border.
* optionally use is-dismissible
to add a closing icon to your message via JavaScript. Its behavior, however, applies only on the current screen. It will not prevent a message from re-appearing once the page re-loads, or another page is loaded.
Don’t use update-nag
as a class name!
It is not suitable for regular admin notices, as it will apply different layout styling to the message. Additionally it will trigger the message to be moved above the page title (<h1>
), thus semantically prioritizing it above other notices which is not likely to be appropriate in a plugin or theme context.
The inner content of the div is the message, and it’s a good idea to wrap the message in a paragraph tag <p>
for the correct amount of padding in the output.
function sample_admin_notice__success() { ?> <div class="notice notice-success is-dismissible"> <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p> </div> <?php } add_action( 'admin_notices', 'sample_admin_notice__success' );
function sample_admin_notice__error() { $class = 'notice notice-error'; $message = __( 'Irks! An error has occurred.', 'sample-text-domain' ); printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); } add_action( 'admin_notices', 'sample_admin_notice__error' );
In late 2017, an unofficial defined constant was proposed by LittleBizzy as a voluntary way for plugin or theme authors to allow for hiding certain admin notices that may be considered bothersome by some webmasters. It can be used as follows:
define('DISABLE_NAG_NOTICES', true);
This code snippet, called a “defined constant” in PHP, can be added to your wp-config.php
file, your theme functions.php
file, or using a free plugin like Custom Functions by LittleBizzy or My Custom Functions by Space X-Chimp.
It should be noted that there is not universal support for this constant, although a limited number of plugin and theme authors have began to support it. A typical use case might be for hiding recurring “nag” notices that ask users to review their extension on WordPress.org, etc. Furthermore, it should not have any effect on general notices that appear within WP Admin, and is simply a way for extensions to opt-in to disabling certain notices at their own discretion.
Version | Description |
---|---|
3.1.0 | Introduced. |
© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/hooks/admin_notices