settings_errors

函数


settings_errors ( $setting = '', $sanitize = false, $hide_on_update = false )
参数
  • (string)
    $setting
    Optional slug title of a specific setting whose errors you want.
    Required:
    Default: (empty)
  • (bool)
    $sanitize
    Whether to re-sanitize the setting value before returning errors.
    Required:
    Default: false
  • (bool)
    $hide_on_update
    If set to true errors will not be shown if the settings page has already been submitted.
    Required:
    Default: false
定义位置
  • wp-admin/includes/template.php
    , line 1927
引入
3.0.0
弃用

显示由add_settings_error()注册的设置错误。

是Settings API的一部分。为通过get_settings_errors()检索的每个错误输出一个div。

在提交基于Settings API的设置页面后,会自动调用该功能。错误应该在register_setting()中定义的设置的验证回调函数中添加。

$sanitize选项被传入get_settings_errors(),并将在其当前值上重新运行设置消毒。

$hide_on_update选项将导致错误只在设置页面首次加载时显示。如果用户已经保存了新的值,它将被隐藏,以避免重复提交后默认错误报告中已经显示的信息。这对于在用户到达设置页面时显示一般的错误是很有用的,比如缺少设置。

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 ) {
		if ( 'updated' === $details['type'] ) {
			$details['type'] = 'success';
		}

		if ( in_array( $details['type'], array( 'error', 'success', 'warning', 'info' ), true ) ) {
			$details['type'] = 'notice-' . $details['type'];
		}

		$css_id    = sprintf(
			'setting-error-%s',
			esc_attr( $details['code'] )
		);
		$css_class = sprintf(
			'notice %s settings-error is-dismissible',
			esc_attr( $details['type'] )
		);

		$output .= "
n"; $output .= "

{$details['message']}

"; $output .= "
n"; } echo $output; }