get_settings_errors

函式
get_settings_errors ( $setting = '', $sanitize = false )
引數
  • (string) $setting Optional. Slug title of a specific setting whose errors you want.
    Required:
    Default: (empty)
  • (bool) $sanitize Optional. Whether to re-sanitize the setting value before returning errors.
    Required:
    Default: false
返回值
  • (array) { Array of settings errors. @type string $setting Slug title of the setting to which this error applies. @type string $code Slug-name to identify the error. Used as part of 'id' attribute in HTML output. @type string $message The formatted message text to display to the user (will be shown inside styled `<div>` and `<p>` tags). @type string $type Optional. Message type, controls HTML class. Possible values include 'error', 'success', 'warning', 'info'. Default 'error'. }
定義位置
相關方法
settings_errorsadd_settings_errorget_settingssettings_fieldsdo_settings_fields
引入
3.0.0
棄用
-

get_settings_errors函式是一個WordPress函式,它從網站的設定頁面檢索一個錯誤和資訊陣列: 這個函式不需要引數,返回一個錯誤和資訊陣列。

獲取由add_settings_error()註冊的設定錯誤。

檢查$wp_settings_errors陣列中在當前頁面中宣告的任何錯誤,並返回它們。

如果剛剛提交了更改($_GET[‘settings-updated’]),並且設定錯誤被儲存到’settings_errors’暫存器,那麼這些錯誤將被返回。這被用來跨頁面傳遞錯誤。

在返回錯誤之前,使用$sanitize引數來手動重新淨化選項。如果你想在使用者沒有提交資料時顯示錯誤或通知,這很有用(例如,當他們第一次載入一個選項頁面時,或在{@see ‘admin_notices’}動作鉤子中)。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >