get_settings_errors()

最后更新于:2021-11-27 00:13:40

get_settings_errors( string$setting=”, bool$sanitize=false)

Fetch settings errors registered by add_settings_error().

参数

$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

响应

(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’.

源文件

文件: gc-admin/includes/template.php

function get_settings_errors( $setting = '', $sanitize = false ) {
	global $gc_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' ) ) {
		$gc_settings_errors = array_merge( (array) $gc_settings_errors, get_transient( 'settings_errors' ) );
		delete_transient( 'settings_errors' );
	}

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

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

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

		return $setting_errors;
	}

	return $gc_settings_errors;
}