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
定義位置
相關方法
get_settings_errorsadd_settings_errorsettings_fieldsis_errordo_settings_fields
引入
3.0.0
棄用
-

settings_errors: 這是一個WordPress的函式,顯示一個給定設定頁面的設定錯誤。它通常用於在使用者提交一個無效資料的表單時顯示錯誤資訊: 這個函式有一個可選的引數,就是要顯示錯誤的設定頁面。

顯示由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 .= "<div id='$css_id' class='$css_class'> n";
		$output .= "<p><strong>{$details['message']}</strong></p>";
		$output .= "</div> n";
	}

	echo $output;
}

常見問題

FAQs
檢視更多 >