get_settings_errors()WP 3.0.0

Retrieves messages registered by the function add_settings_error().

This function is one of the group of functions Settings API. And is a helper function for settings_errors().

Checks the $wp_settings_errors array for messages (errors) added during the page load and returns them.

We can save messages into a transient option: set_transient( 'settings_errors' ), so that by using get_transient(), on page reload or a new visit to the settings page, we can retrieve errors from previous data validations. WordPress recognizes this transient and if the GET parameter ?settings-updated=true is present (it is set automatically on successful options saving), then our transient containing the saved messages will be merged with the current messages array and the transient settings_errors will be deleted. Here is the part of the get_settings_errors() function responsible for these actions:

// If settings were passed back from options.php then use them
if (
	isset( $_GET['settings-updated'] ) &&
	$_GET['settings-updated'] &&
	get_transient( 'settings_errors' )
)
{
	$wp_settings_errors = array_merge(
		(array) $wp_settings_errors,
		get_transient( 'settings_errors' )
	);

	delete_transient( 'settings_errors' );
}

No Hooks.

Returns

Array[]. Array containing message data:

array(
	'setting' =>  $setting,
	'code'    =>  $code,
	'message' =>  $message,
	'type'    =>  $type
);

Usage

get_settings_errors( $setting, $sanitize );
$setting(string)
The identifier that should match the $setting parameter from add_settings_error(). The specified group of errors will be retrieved. If no identifier is provided, the function will get all registered errors.
Default: ''
$sanitize(boolean)
Whether to sanitize the value of the specified option: true (yes, sanitize again) or false (no, do not sanitize). Specify the parameter $sanitaze = true to trigger the sanitize_option() function — it sanitizes the option using the filter specified in sanitize_option (sanitization for unknown option functions must be registered separately; see the description of sanitize_option()). If the option fails validation, an error message is automatically registered. This can be useful when you need to validate form data before it is saved, for example, on a simple visit to the settings page. Messages are displayed whenever there are registered messages in the errors array.
Default: false

Examples

0

#1 An example of how the function works

Display the messages for the "my_option_name" option that were registered when trying to save the option. If the value of the option failed, the error messages were registered by the add_settings_error() function, when saving the options. After the form data has been sent, we can get these messages with get_settings_errors() function and output them with admin_notices hook. To do that, we write our own function which processes the result of get_settings_errors() and outputs all the messages on the screen:

add_action( 'admin_notices', 'your_admin_notices_action' );

function your_admin_notices_action() {

	$settings_errors = get_settings_errors( 'my_option_name' );

	if ( empty( $settings_errors ) ){
		return;
	}

	$output = '';
	foreach ( $settings_errors as $key => $details ) {

		$css_id = 'setting-error-' . $details['code'];
		$css_class = $details['type'] . ' settings-error';
		$output .= "<div id='$css_id' class='$css_class'> n";
		$output .= "<p><strong>{$details['message']}</strong></p>";
		$output .= "</div> n";
	}

	echo $output;
}

This is just a demonstration of how the function works. Registered messages are automatically output by WordPress when you save data, you just need to check the data before saving and register messages with add_settings_error() if necessary. Separately displaying messages, as in this example, is usually not necessary.

Notes

  • Global. Array[]. $wp_settings_errors Storage array of errors registered during this pageload

Changelog

Since 3.0.0 Introduced.

get_settings_errors() code WP 6.9

function get_settings_errors( $setting = '', $sanitize = false ) {
	global $wp_settings_errors;

	/*
	 * If $sanitize is true, manually re-run the sanitization for this option
	 * This allows the $sanitize_callback from register_setting() to run, adding
	 * any settings errors you want to show by default.
	 */
	if ( $sanitize ) {
		sanitize_option( $setting, get_option( $setting ) );
	}

	// If settings were passed back from options.php then use them.
	if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] && get_transient( 'settings_errors' ) ) {
		$wp_settings_errors = array_merge( (array) $wp_settings_errors, get_transient( 'settings_errors' ) );
		delete_transient( 'settings_errors' );
	}

	// Check global in case errors have been added on this pageload.
	if ( empty( $wp_settings_errors ) ) {
		return array();
	}

	// Filter the results to those of a specific setting if one was set.
	if ( $setting ) {
		$setting_errors = array();

		foreach ( (array) $wp_settings_errors as $key => $details ) {
			if ( $setting === $details['setting'] ) {
				$setting_errors[] = $wp_settings_errors[ $key ];
			}
		}

		return $setting_errors;
	}

	return $wp_settings_errors;
}