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.
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 stringOptional. The type of admin notice. For example, 'error', 'success', 'warning', 'info'.$dismissiblebooldefault: falseOptional. Whether the admin notice is dismissible.$idstringdefault: empty stringOptional. The value of the admin notice's ID attribute.$additional_classesstring[]default: empty arrayOptional. A string array of class names.$attributesstring[]default: empty arrayOptional. Additional attributes for the notice div.$paragraph_wrapbooldefault: trueOptional. 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?
- Why doesn't hooking the wp_admin_notice action let me change the notice text?
- Why are some tags or attributes missing from my notice message?
- Why does my message end up wrapped in an extra pair of paragraph tags?
Why does my notice print in the wrong place instead of near the top of the admin page?
Why doesn't hooking the wp_admin_notice action let me change the notice text?
Why are some tags or attributes missing from my notice message?
Why does my message end up wrapped in an extra pair of paragraph tags?
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
- Scaling
- Constant
- Instructions
- 16
- Plugin surface
- 1 hook
- Called by
- 44
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 16.
Third-party callbacks on 'wp_admin_notice' run inside this call, and their cost is not bounded by anything here.
44 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 16 | do_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:
- do_action( wp_admin_notice )actionline 9316 (+9 into the body)
Fires before an admin notice is output.
Uses · 3
- do_action()Calls the callback functions that have been added to an action hook.
- wp_kses_post()Sanitizes content for allowed HTML tags for post content.
- wp_get_admin_notice()Creates and returns the markup for an admin notice.
Used by · 44
- Bulk_Upgrader_Skin::after()Performs an action following a bulk update.
- Custom_Background::admin_page()Displays the custom background page.
- Custom_Image_Header::step_1()Displays first step of custom header image page.
- WP_Customize_Theme_Control::content_template()Render a JS template for theme display.
- WP_MS_Themes_List_Table::column_autoupdates()Handles the auto-updates column output.
- WP_MS_Themes_List_Table::column_description()Handles the description column output.
- WP_Plugin_Dependencies::display_admin_notice_for_circular_dependencies()Displays an admin notice if circular dependencies are installed.
- WP_Plugin_Dependencies::display_admin_notice_for_unmet_dependencies()Displays an admin notice if dependencies are not installed.
- WP_Plugin_Install_List_Table::display_rows()Generates the list table rows.
- WP_Plugin_Install_List_Table::no_items()
- WP_Plugins_List_Table::single_row()Generates the markup for a single plugin row.
- WP_Posts_List_Table::inline_edit()Outputs the hidden row displayed when inline editing
Show all 44
- WP_Privacy_Policy_Content::notice()Adds a notice with a link to the guide when editing the privacy policy page.
- WP_Privacy_Policy_Content::policy_text_changed_notice()Outputs a warning when some privacy info has changed.
- WP_Terms_List_Table::inline_edit()Outputs the hidden row displayed when inline editing
- WP_Widget_Media_Audio::render_control_template_scripts()Render form template scripts.
- WP_Widget_Media_Image::render_control_template_scripts()Render form template scripts.
- WP_Widget_Media_Video::render_control_template_scripts()Render form template scripts.
- WP_Widget_Text::form()Outputs the Text widget settings form.
- _local_storage_notice()Outputs the HTML for restoring the post data from DOM storage
- _wp_posts_page_notice()Outputs a notice when editing the page for posts (internal use only).
- core_auto_updates_settings()Display WordPress auto-updates settings.
- core_upgrade_preamble()Display upgrade WordPress for downloading latest or upgrading automatically form.
- deactivated_plugins_notice()Renders an admin notice when a plugin was deactivated during an update.
- default_password_nag()
- do_meta_boxes()Meta-Box template function.
- install_plugin_information()Displays plugin information in dialog box form.
- login_header()Outputs the login page header.
- maintenance_nag()Displays maintenance nag HTML message.
- network_step1()Prints step 1 for Network installation process.
- network_step2()Prints step 2 for Network installation process.
- new_user_email_admin_notice()Adds an admin notice alerting the user to check for confirmation request email after email address change.
- paused_plugins_notice()Renders an admin notice in case some plugins have been paused due to errors.
- paused_themes_notice()Renders an admin notice in case some themes have been paused due to errors.
- post_submit_meta_box()Displays post submit form fields.
- request_filesystem_credentials()Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.
- site_admin_notice()Displays an admin notice to upgrade all sites after a core upgrade.
- update_nag()Returns core update notification message.
- wp_comment_reply()Outputs the in-line comment reply-to form in the Comments list table.
- wp_dashboard_quick_press()Displays the Quick Draft widget.
- wp_import_upload_form()Outputs the form used by the importers to accept the data to be imported.
- wp_print_community_events_markup()Prints the markup for the Community Events section of the Events and News Dashboard widget.
- wp_print_media_templates()Prints the templates used in the media manager.
- wp_recovery_mode_nag()Displays a notice when the user is in recovery mode.
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.
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.