wp_get_admin_notice()WP 6.4.0

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>
1 time — 0.000071 sec (very fast) | 50000 times — 0.10 sec (speed of light)

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 the type parameter is specified, for example, if type=warning, then the class notice-warning will be added.
    • notice-alt - the background will be colored in a color corresponding to the type.
    • is-dismissible - added if the dismissible parameter 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' ].
  • paragraph_wrap(true|false)
    Optional parameter. Whether to wrap the notice message in a <p> tag.
    Default: true

Examples

0

#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.

wp_get_admin_notice() code WP 7.0

function wp_get_admin_notice( $message, $args = array() ) {
	$defaults = array(
		'type'               => '',
		'dismissible'        => false,
		'id'                 => '',
		'additional_classes' => array(),
		'attributes'         => array(),
		'paragraph_wrap'     => true,
	);

	$args = wp_parse_args( $args, $defaults );

	/**
	 * Filters the arguments for an admin notice.
	 *
	 * @since 6.4.0
	 *
	 * @param array  $args    The arguments for the admin notice.
	 * @param string $message The message for the admin notice.
	 */
	$args       = apply_filters( 'wp_admin_notice_args', $args, $message );
	$id         = '';
	$classes    = 'notice';
	$attributes = '';

	if ( is_string( $args['id'] ) ) {
		$trimmed_id = trim( $args['id'] );

		if ( '' !== $trimmed_id ) {
			$id = 'id="' . $trimmed_id . '" ';
		}
	}

	if ( is_string( $args['type'] ) ) {
		$type = trim( $args['type'] );

		if ( str_contains( $type, ' ' ) ) {
			_doing_it_wrong(
				__FUNCTION__,
				sprintf(
					/* translators: %s: The "type" key. */
					__( 'The %s key must be a string without spaces.' ),
					'<code>type</code>'
				),
				'6.4.0'
			);
		}

		if ( '' !== $type ) {
			$classes .= ' notice-' . $type;
		}
	}

	if ( true === $args['dismissible'] ) {
		$classes .= ' is-dismissible';
	}

	if ( is_array( $args['additional_classes'] ) && ! empty( $args['additional_classes'] ) ) {
		$classes .= ' ' . implode( ' ', $args['additional_classes'] );
	}

	if ( is_array( $args['attributes'] ) && ! empty( $args['attributes'] ) ) {
		$attributes = '';
		foreach ( $args['attributes'] as $attr => $val ) {
			if ( is_bool( $val ) ) {
				$attributes .= $val ? ' ' . $attr : '';
			} elseif ( is_int( $attr ) ) {
				$attributes .= ' ' . esc_attr( trim( $val ) );
			} elseif ( $val ) {
				$attributes .= ' ' . $attr . '="' . esc_attr( trim( $val ) ) . '"';
			}
		}
	}

	if ( false !== $args['paragraph_wrap'] ) {
		$message = "<p>$message</p>";
	}

	$markup = sprintf( '<div %1$sclass="%2$s"%3$s>%4$s</div>', $id, $classes, $attributes, $message );

	/**
	 * Filters the markup for an admin notice.
	 *
	 * @since 6.4.0
	 *
	 * @param string $markup  The HTML markup for the admin notice.
	 * @param string $message The message for the admin notice.
	 * @param array  $args    The arguments for the admin notice.
	 */
	return apply_filters( 'wp_admin_notice_markup', $markup, $message, $args );
}