wp_get_admin_notice()
Forms and returns the HTML code of a notice (message, error) that we usually see at the top of the admin panel.
Use wp_admin_notice() when you need to immediately display the result on the screen, rather than getting it in a variable.
This function is typically called on one of the hooks:
In addition to these hooks, the function can be called anywhere in HTML (in the admin area). In this case, the notice will be moved under the header by a JS script. For example, if at the time the code is executed, the hooks admin_notices have already run, but we still need to display a notice, we can output it directly in the content or in the footer. Then the JS script will collect all notices (in the order of output) and place them under the header:
add_action( 'admin_head', function () {
wp_admin_notice( 'Output on the admin_head hook, that is, at the very top.' );
} );
add_action( 'admin_footer', function () {
wp_admin_notice( 'Output on the admin_footer hook, that is, at the very bottom.' );
} );
add_action( 'shutdown', function () {
wp_admin_notice( 'And I am the very last hook in the system, but I will still be displayed under the header!' );
} );
The result of the code execution:
Previously, the HTML of such a notice had to be written manually and was usually output on the hook admin_notices. Now there is a standard.
An example of what notices look like:
HTML template for a simple message (without additional arguments):
<div class="{class}"><p>{message}</p></div>
Hooks from the function
Returns
String. HTLM code of the admin notice.
Usage
wp_get_admin_notice( $message, $args );
- $message(string) (required)
- Text of the message (notice). HTML can be used. Any content passed is filtered by the function wp_kses_post().
- $args(array)
Array of arguments for customizing the notice.
Default: empty array
-
type(string)
Type of notice, for example:success— success.error— error.warning— warning.info— information.''— default - if not specified.
Default: empty string
-
dismissible(true|false)
Whether the notice can be closed (hidden) by clicking the cross. The choice is not remembered, meaning that after reloading the page, the notice will be displayed again.
Default: false -
id(string)
Value of the ID attribute for the HTML container of the notice.
Default: empty string -
additional_classes(string[])
Array of CSS classes for the HTML container of the notice. These classes will be added to the classes from the list:notice- base class, always present.notice-{type}is added if thetypeparameter is specified, for example, if type=warning, then the classnotice-warningwill be added.notice-alt- the background will be colored in a color corresponding to the type.is-dismissible- added if thedismissibleparameter is specified.inline- the notice will not be moved by the JS script under the header.
Default: empty array
-
attributes(array)
Additional attributes to the main <div> tag.
You need to specify an array like[ 'attribute_name' => 'value' ].
Value can be:- bool - to add or not add the attribute name in HTML (without value). For example:
[ 'hidden' => true ]. - int|string - will simply be cleaned and added as a value. For example:
[ 'data-foo' => 'bar' ].
- bool - to add or not add the attribute name in HTML (without value). For example:
- paragraph_wrap(true|false)
Optional parameter. Whether to wrap the notice message in a<p>tag.
Default: true
-
Examples
#1 Collect the tags and display them
$messages = [];
if( 'some condition' ){
$messages[] = wp_get_admin_notice( 'Note 1', [ 'type' => 'error' ] );
}
if( 'some condition' ){
$messages[] = wp_get_admin_notice( 'Note 2', [ 'type' => 'error' ] );
}
// some code
echo implode( "\n", $messages );
Changelog
| Since 6.4.0 | Introduced. |

