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’}动作钩子中)。

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
查看更多 >