settings_errors( string $setting = '', bool $sanitize = false, bool $hide_on_update = false )
- Since
- 3.0.0, 5.3.0
- Source
wp-admin/includes/template.php:1983
Description
Part of the Settings API. Outputs a div for each error retrieved by get_settings_errors().
This is called automatically after a settings page based on the Settings API is submitted. Errors should be added during the validation callback function for a setting defined in register_setting().
The $sanitize option is passed into get_settings_errors() and will re-run the setting sanitization on its current value.
The $hide_on_update option will cause errors to only show when the settings page is first loaded. if the user has already saved new values it will be hidden to avoid repeating messages already shown in the default error reporting after submission. This is useful to show general errors like missing settings when the user arrives at the settings page.
Compatibility
- WordPress
- since 5.3.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
$settingstringoptional- Optional slug title of a specific setting whose errors you want.Default:
'' $sanitizebooloptional- Whether to re-sanitize the setting value before returning errors.Default:
false $hide_on_updatebooloptional- If set to true errors will not be shown if the settings page has already been submitted.Default:
false
Performance profile
How much work a call to settings_errors() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 8–20
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()one call below settings_errors() - transienttransient
get_transient()one call below settings_errors() - hookthird-party callbacks
apply_filters()one call below settings_errors()
Further down the call graph this can also reach cache, serialize and query. 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 settings_errors() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($value) | 8 | none |
| always | 12–20 | get_settings_errors() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 64 | 8–20 | 7 | |
| 8.5 | 64 | 8–20 | 7 | |
| 8.4 | 64 | 8–20 | 7 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 68 | 8–20 | 7 | |
| 8.2 | 68 | 8–20 | 7 | |
| 8.1 | 68 | 8–20 | 7 | |
| 7.4 | 68 | 8–20 | 7 |
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.
Uses · 2
- get_settings_errors()Fetches settings errors registered by add_settings_error().
- esc_attr()Escaping for HTML attributes.
Used by · 1
- twentyeleven_theme_options_render_page()Renders the theme options page for Twenty Eleven.
Source code
function settings_errors( $setting = '', $sanitize = false, $hide_on_update = false ) { if ( $hide_on_update && ! empty( $_GET['settings-updated'] ) ) { return; } $settings_errors = get_settings_errors( $setting, $sanitize ); if ( empty( $settings_errors ) ) { return; } $output = ''; foreach ( $settings_errors as $key => $details ) { if ( 'updated' === $details['type'] ) { $details['type'] = 'success'; } if ( in_array( $details['type'], array( 'error', 'success', 'warning', 'info' ), true ) ) { $details['type'] = 'notice-' . $details['type']; } $css_id = sprintf( 'setting-error-%s', esc_attr( $details['code'] ) ); $css_class = sprintf( 'notice %s settings-error is-dismissible', esc_attr( $details['type'] ) ); $output .= "<div id='$css_id' class='$css_class'> \n"; $output .= "<p><strong>{$details['message']}</strong></p>"; $output .= "</div> \n"; } echo $output;}Changelog
Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
error and updated CSS classes are mapped to notice-error and notice-success.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/template.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.