settings_errors()

settings_errors( string $setting = ”, bool $sanitize = …

settings_errors( string $setting = , bool $sanitize = false, bool $hide_on_update = false )

显示由add_settings_error()注册的设置错误。
Display settings errors registered by add_settings_error().

目录锚点:#说明#参数#源码#笔记


说明(Description)

设置API的一部分。为get_settings_errors()检索到的每个错误输出一个div。在提交基于设置API的设置页后,将自动调用此函数。对于在register_setting()中定义的设置,应在验证回调函数期间添加错误。$sanitize选项被传递到get_settings_errors()中,并将对其当前值重新运行设置清理。$hideu on_update选项将导致错误仅在首次加载设置页时显示。如果用户已经保存了新值,它将被隐藏,以避免重复提交后默认错误报告中已经显示的消息。这对于在用户到达设置页时显示诸如缺少设置之类的常规错误非常有用。


参数(Parameters)

参数 类型 说明
$setting (string) 指定错误所在的特定设置的标题。
$sanitize (bool) 是否在返回错误之前重新清理设置值。
$hide_on_update (bool) 如果设置为true,则如果已提交设置页,则不会显示错误。

源码(Source)

/**
 * Display settings errors registered by {@see add_settings_error()}.
 *
 * Part of the Settings API. Outputs a div for each error retrieved by
 * {@see get_settings_errors()}.
 *
 * This is called automatically after a settings page based on the
 * Settings API is submitted. Errors should be added during the validation
 * callback function for a setting defined in {@see register_setting()}
 *
 * The $sanitize option is passed into {@see get_settings_errors()} and will
 * re-run the setting sanitization
 * on its current value.
 *
 * The $hide_on_update option will cause errors to only show when the settings
 * page is first loaded. if the user has already saved new values it will be
 * hidden to avoid repeating messages already shown in the default error
 * reporting after submission. This is useful to show general errors like
 * missing settings when the user arrives at the settings page.
 *
 * @since 3.0.0
 *
 * @param string $setting        Optional slug title of a specific setting who's errors you want.
 * @param bool   $sanitize       Whether to re-sanitize the setting value before returning errors.
 * @param bool   $hide_on_update If set to true errors will not be shown if the settings page has already been submitted.
 */
function settings_errors( $setting = '', $sanitize = false, $hide_on_update = false ) {

	if ( $hide_on_update && ! empty( $_GET['settings-updated'] ) )
		return;

	$settings_errors = get_settings_errors( $setting, $sanitize );

	if ( empty( $settings_errors ) )
		return;

	$output = '';
	foreach ( $settings_errors as $key => $details ) {
		$css_id = 'setting-error-' . $details['code'];
		$css_class = $details['type'] . ' settings-error notice is-dismissible';
		$output .= " 
";
		$output .= "{$details['message']}";
		$output .= " 
";
	}
	echo $output;
}
更新版本 源码位置 使用 被使用
5.3.0 wp-admin/includes/template.php 8 1

笔记(Notes)

例子

类别:WordPress 函数手册

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册