wppaste
WordPress

wp_get_admin_notice( string $message, array $args = array() ): string

Since
6.4.0
Source
wp-includes/functions.php:9196
Creates and returns the markup for 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.
$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.

Return value

string
The markup for an admin notice.

Performance profile

How much work a call to wp_get_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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
50–95

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 124.

Plugin surface
2 hooks

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

Called by
7

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

What it touches

  • hookthird-party callbacksapply_filters()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 · 2 distinct outcomes

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

WhenInstructionsCalls it makes
always50–83wp_parse_args(), apply_filters(), sprintf(), apply_filters()
$type68–95wp_parse_args(), apply_filters(), __(), sprintf(), _doing_it_wrong(), sprintf(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12450–9517
8.512450–9517
8.412450–951716 fewer instructions than PHP 8.3
8.314050–10717
8.214050–10717
8.114050–107171 fewer instruction than PHP 7.4
7.414150–10717

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 · 2

2 hooks fire while wp_get_admin_notice() runs, in this order:

  1. apply_filters( wp_admin_notice_args )filterline 9216 (+20 into the body)

    Filters the arguments for an admin notice.

  2. apply_filters( wp_admin_notice_markup )filterline 9285 (+89 into the body)

    Filters the markup for an admin notice.

Uses · 6

Used by · 7

Source code

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 );

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.