WordPress at Your Fingertips

get_settings_errors() WP 1.0

Fetch settings errors registered by add_settings_error()

{{desc}}

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

Notes

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

Code of get_settings_errors() WP 5.8.3

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

Settings API

No comments
    Log In