get_settings_errors() WordPress Function

The get_settings_errors() function is a WordPress core function that retrieves all settings errors registered by add_settings_error(). This function is useful for displaying all settings errors in a single place, such as in the admin panel.

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

Fetches settings errors registered by add_settings_error().


Description

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


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


Top ↑

Return

(array) Array of settings errors.

  • 'setting'
    (string) Slug title of the setting to which this error applies.
  • 'code'
    (string) Slug-name to identify the error. Used as part of 'id' attribute in HTML output.
  • 'message'
    (string) The formatted message text to display to the user (will be shown inside styled <div> and <p> tags).
  • 'type'
    (string) Optional. Message type, controls HTML class. Possible values include 'error', 'success', 'warning', 'info'. Default 'error'.


Top ↑

Source

File: wp-admin/includes/template.php

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


Top ↑

Changelog

Changelog
VersionDescription
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.