wppaste
WordPress

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
Displays settings errors registered by add_settings_error().

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

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

Instructions
8–20

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • optionoption read or writeget_option()one call below settings_errors()
  • transienttransientget_transient()one call below settings_errors()
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!empty($value)8none
always12–20get_settings_errors()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev648–207
8.5648–207
8.4648–2074 fewer instructions than PHP 8.3
8.3688–207
8.2688–207
8.1688–207
7.4688–207

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

Used by · 1

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.

  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.

5.3.0
Legacy error and updated CSS classes are mapped to notice-error and notice-success.from the docblock
3.0.0
Introduced.from the docblock

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