1. 首页
  2. WordPress 函数手册

get_settings_errors()

get_settings_errors( string $setting = , boolean $sanitize = false )

获取由add_settings_error()注册的设置错误。
Fetch settings errors registered by add_settings_error().

目录锚点:#说明#参数#返回#源码


说明(Description)

检查$wp_settings_errors数组中当前页面加载期间声明的任何错误并返回它们。

如果更改刚刚提交($u GET[‘settings-updated’]),并且设置错误被保存到’settingsu errors’瞬态,则这些错误将被返回。这用于跨页面加载传递错误。

使用$sanitize参数在返回错误之前手动重新清理选项。如果您有错误或通知,甚至在用户尚未提交数据时(即当他们第一次加载选项页或在“管理通知”操作挂钩中)也要显示,则此功能非常有用。


参数(Parameters)

参数 类型 必填 说明
$setting (string) 可选 你想要错误的特定设置的slug标题。
$sanitize (boolean) 可选 是否在返回错误之前重新清理设置值。

返回(Return)

(array)设置错误数组。


源码(Source)

/**
 * 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 admin_notices action hook)
 *
 * @since 3.0.0
 *
 * @global array $wp_settings_errors Storage array of errors registered during this pageload
 *
 * @param string $setting Optional slug title of a specific setting who's errors you want.
 * @param boolean $sanitize Whether to re-sanitize the setting value before returning errors.
 * @return array Array of settings errors
 */
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 ( ! count( $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;
}
更新版本 源码位置 使用 被使用
3.0.0 wp-admin/includes/template.php:1757 1 function 4

get_settings_errors() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_settings_errors/

发表评论

您的电子邮箱地址不会被公开。