wppaste
WordPress

wp_admin_notice( string $message, array $args = array() )

Since
6.4.0
Source
wp-includes/functions.php:9307

Echoes a ready-to-print admin notice div for $message, letting $args control its type, dismissibility, ID, extra classes, attributes, and paragraph wrapping. It sanitizes the final markup with wp_kses_post() before printing, so disallowed HTML in $message gets stripped. Because it echoes directly and returns nothing, it needs to run inside an admin-rendering context such as the admin_notices hook rather than being called for its return value.

Outputs an admin notice.

Compatibility

WordPress
since 6.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$messagestring
The message to output.
$argsarrayoptional
An array of arguments for the admin notice. Default empty array.Default: array()
  • $typestringdefault: empty string

    Optional. The type of admin notice. For example, 'error', 'success', 'warning', 'info'.
  • $dismissiblebooldefault: false

    Optional. Whether the admin notice is dismissible.
  • $idstringdefault: empty string

    Optional. The value of the admin notice's ID attribute.
  • $additional_classesstring[]default: empty array

    Optional. A string array of class names.
  • $attributesstring[]default: empty array

    Optional. Additional attributes for the notice div.
  • $paragraph_wrapbooldefault: true

    Optional. Whether to wrap the message in paragraph tags.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Show a dismissible info notice with a post count

Print a one-off admin notice built from the published post count, run directly rather than through a hook.

$counts = wp_count_posts();

wp_admin_notice(
	sprintf( 'You currently have %d published posts.', (int) $counts->publish ),
	array(
		'type'        => 'info',
		'dismissible' => true,
	)
);

wp_admin_notice() echoes immediately, so anything printed before this line in the sandbox will appear above the notice markup.

Warn on the admin_notices hook when posts are missing a meta value

Query posts that have no price meta and list them in a dismissible warning notice with a custom ID and class, the normal way this function is used in wp-admin.

add_action( 'admin_notices', 'sandbox_missing_price_notice' );

function sandbox_missing_price_notice() {
	$query = new WP_Query(
		array(
			'post_type'      => 'post',
			'posts_per_page' => -1,
			'meta_query'     => array(
				array(
					'key'     => 'price',
					'compare' => 'NOT EXISTS',
				),
			),
		)
	);

	if ( ! $query->have_posts() ) {
		return;
	}

	$titles = wp_list_pluck( $query->posts, 'post_title' );

	wp_admin_notice(
		sprintf( 'These posts have no price set: %s', esc_html( implode( ', ', $titles ) ) ),
		array(
			'type'               => 'warning',
			'dismissible'        => true,
			'id'                 => 'missing-price-notice',
			'additional_classes' => array( 'inline' ),
		)
	);
}

Post 2 already has the price meta from the sandbox fixtures, so posts 1, 3, 4, and 5 are the ones listed.

Common problems and fixes · 4

Why does my notice print in the wrong place instead of near the top of the admin page?

wp_admin_notice() echoes the notice the instant it runs, it does not queue markup for later. If you call it during page setup instead of during the admin_notices (or network_admin_notices) action, the HTML lands wherever your code happens to execute.

Why doesn't hooking the wp_admin_notice action let me change the notice text?

wp_admin_notice fires that action with do_action(), passing $message and $args purely for observation. The function reads its own local $message and $args on the very next line to build the markup, so a callback on that action cannot alter what gets displayed.

Why are some tags or attributes missing from my notice message?

The final markup is run through wp_kses_post() before being echoed, which only allows the same HTML that is permitted in post content. Script tags, iframes, and many custom attributes are stripped silently.

Why does my message end up wrapped in an extra pair of paragraph tags?

The $paragraph_wrap argument defaults to true, so the message is wrapped in a

element automatically. If your $message already contains its own

tags, you get nested paragraphs.

Alternatives and related functions

wp_get_admin_notice
When you need the notice markup as a string, for example to insert it into a larger HTML block, instead of having it echoed immediately.
add_settings_error
When the notice is tied to a Settings API page and should be queued alongside validation errors rather than printed on the spot.
settings_errors
When you need to output the notices previously queued with add_settings_error() at a specific point in a settings page.

Performance profile

How much work a call to wp_admin_notice() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
16

Executed per call on PHP 8.5. The body compiles to 16.

Plugin surface
1 hook

Third-party callbacks on 'wp_admin_notice' run inside this call, and their cost is not bounded by anything here.

Called by
44

44 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_admin_notice() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always16do_action(), wp_get_admin_notice(), wp_kses_post()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 16 instructions, 16 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_admin_notice() runs, in this order:

  1. do_action( wp_admin_notice )actionline 9316 (+9 into the body)

    Fires before an admin notice is output.

Uses · 3

Used by · 44

Show all 44

Source code

function wp_admin_notice( $message, $args = array() ) {	/**	 * Fires before an admin notice is output.	 *	 * @since 6.4.0	 *	 * @param string $message The message for the admin notice.	 * @param array  $args    The arguments for the admin notice.	 */	do_action( 'wp_admin_notice', $message, $args ); 	echo wp_kses_post( wp_get_admin_notice( $message, $args ) );}

Changelog

Introduced in 6.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.