Prints admin screen notices.
More Information
Example
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' );
Source
do_action( 'admin_notices' );
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Sample Usage
The class
notice-successwill display the message with a white background and a green left border.The class
notice-errorwill display the message with a white background and a red left border.Use
notice-warningfor a yellow/orange, andnotice-infofor a blue left border.The class name
is-dismissiblewill 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.[copied/pasted from https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices%5D
.notice-alt(gives the notice a background hue matching the color of.notice-info,.notice-success, etc),.notice-title(formats the element as a title for the notice, useful to blend in with the WP style),.notice-large(increases the size of the notification box as well as its padding). These styles were added in WordPress 4.4.Method to display message dynamically (Taking example of warning message)
Now you can simply initialize the class to display a dynamic message.
/** * Class Log_Warning * * Displays admin notices in the WordPress dashboard. */ class Log_Warning { /** * Message text for the notice. * * @var string */ private string $message; /** * Notice type (info, error, success, warning). * * @var string */ private string $notice = 'info'; /** * Whether the notice can be dismissed. * * @var bool */ private bool $dismissible = false; /** * Constructor. * * @param string $message The message to display. * @param string $notice Type of notice (default 'info'). * @param bool $dismissible Whether notice is dismissible. */ public function __construct( string $message, string $notice = 'info', bool $dismissible = false ) { $this->message = $message; $this->notice = $notice; $this->dismissible = $dismissible; // Hook the render method into the admin_notices action. add_action( 'admin_notices', array( $this, 'render' ) ); } /** * Outputs the admin notice markup. * * @return void */ public function render() { printf( '%s', esc_html( $this->notice ), esc_html( $this->dismissible ? 'is-dismissible' : '' ), esc_html( $this->message ) ); } }As of WordPress 6.4, you can output admin notice/messages with two new functions (which are yet to be documented):
wp_admin_notice()andwp_get_admin_notice();First parameter is your message as a string; second parameter is an args array with the most useful being type:
[ 'type' => 'error' ].So for example:
More information:
https://make.wordpress.org/core/2023/10/16/introducing-admin-notice-functions-in-wordpress-6-4/
The Theme Review Team (TRT) released a package you can use in your theme or plugin to create admin notices:
https://github.com/WPTRT/admin-notices
Its primary purpose is for providing a standardized method of creating admin notices in a consistent manner using the default WordPress styles.
Notices created using this method are automatically dismissible.
Information on the unofficial, voluntary flag
'DISABLE_NAG_NOTICES'is missing. Here are relevant details from the old codex page:Usage sample:
In order to display a notice, echo a div with the class
noticeand one of the following classes:*
notice-error– will display the message with a red left border.*
notice-warning– will display the message with a yellow/orange left border.*
notice-success– will display the message with a green left border.*
notice-info– will display the message with a blue left border.* optionally use
is-dismissibleto 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-nagas 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, 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 for the correct amount of padding in the output.
Here’s a simple way to add an independence day notice to some admin pages.
The class notice-success will display the message with a white background and a green left border and the class notice-error will display the message with a white background and a red left border.
if you want to display the errors you can also use notice-warning for a yellow/orange for a blue left border.
The
admin_noticeshook doesn’t do anything on a Block Editor screen. For that reason you need to print it second time with JS.Here is an example, of how to make a notification that will show on Post Edit Screen if Gutenberg is used:
Here is code to display a notice after a page refresh(for example after a post has been saved/updated).
Usage
Add this action at the top level of your code:
Then use the class anywhere in your code: