WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/admin notices

Description

Notices displayed near the top of admin pages. The hook function should echo a message to be displayed.

Usage

<div class="{class}"><p>{message}</p></div>

Parameters

{class}
(string) (required) The class of admin notice. Should be notice plus any one of notice-error, notice-warning, notice-success, or notice-info. Optionally use is-dismissible to apply a closing icon.
Default: None
{message}
(string) (required) Message to show to user
Default: None

Example

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

The class notice-success will display the message with a white background and a green left border.

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

The class notice-error will display the message with a white background and a red left border.

Use notice-warning for a yellow/orange, and notice-info for a blue left border.

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 class name is-dismissible will automatically trigger a closing icon to be added 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.

Disable Nag Notices

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.

Related