WordPress.org

Codex

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

Plugin API/Action Reference/admin notices

This page is marked as incomplete. You can help Codex by expanding it.

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>', $class, $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.

Related