settings_errors() WordPress Function

The settings_errors() function is a Wordpress function that is used to display messages in the WP admin area. This function is used to display errors, warnings, and notifications.

settings_errors( string $setting = '', bool $sanitize = false, bool $hide_on_update = false ) #

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.


Top ↑

Parameters

$setting

(string)(Optional)slug title of a specific setting whose errors you want.

Default value: ''

$sanitize

(bool)(Optional)Whether to re-sanitize the setting value before returning errors.

Default value: false

$hide_on_update

(bool)(Optional)If set to true errors will not be shown if the settings page has already been submitted.

Default value: false


Top ↑

Source

File: wp-admin/includes/template.php

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


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Legacy error and updated CSS classes are mapped to notice-error and notice-success.
3.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.