函数文档

get_settings_errors()

💡 云策文档标注

概述

get_settings_errors() 函数用于获取通过 add_settings_error() 注册的设置错误信息。它检查当前页面加载期间声明的错误,并可跨页面传递错误,支持按特定设置筛选和重新清理选项值。

关键要点

  • 函数从 $wp_settings_errors 全局数组中检索错误,或从 'settings_errors' 瞬态中获取提交后的错误。
  • 参数 $setting 可选,用于指定要获取错误的设置 slug;$sanitize 可选,控制是否在返回错误前重新清理选项值。
  • 返回一个数组,包含错误详情,如 setting、code、message 和 type 字段。

代码示例

add_action( 'admin_notices', function () : void {
    $errors = get_settings_errors();
    print_r( $errors );
} );

注意事项

  • 在 'admin_notices' 钩子中使用时,可显示默认错误或通知,无需用户提交数据。
  • 相关函数包括 sanitize_option()、get_transient()、delete_transient() 和 get_option()。

📄 原文内容

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

Parameters

$settingstringoptional
Slug title of a specific setting whose errors you want.
$sanitizebooloptional
Whether to re-sanitize the setting value before returning errors.

Default:false

Return

array[] Array of settings error arrays.

  • ...$0 array
    Associative array of setting error data.

    • 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'.

Source

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

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.