get_settings_errors()WP 3.0.0

Fetch settings errors registered by add_settings_error()

Checks the $wp_settings_errors array for any errors declared during the current pageload and returns them.

If changes were just submitted ($_GET['settings-updated']) and settings errors were saved to the 'settings_errors' transient then those errors will be returned instead. This is used to pass errors back across pageloads.

Use the $sanitize argument to manually re-sanitize the option before returning errors. This is useful if you have errors or notices you want to show even when the user hasn't submitted data (i.e. when they first load an options page, or in the admin_notices action hook).

No Hooks.

Return

Array. Array of settings errors.

Usage

get_settings_errors( $setting, $sanitize );
$setting(string)
Slug title of a specific setting whose errors you want.
Default: ''
$sanitize(true|false)
Whether to re-sanitize the setting value before returning errors.
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.1.1

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