admin_notices WordPress Action Hook
The admin_notices hook is a versatile hook that can be used to display any kind of message to the administrator of a WordPress site. This hook is generally used to display important messages or notifications to the administrator, such as updates or security warnings.
do_action( 'admin_notices' ) #
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
Changelog
Version | Description |
---|---|
3.1.0 | Introduced. |