获取add_settings_error()注册的设置错误

原型

get_settings_errors( string $setting = '', boolean $sanitize = false )

描述

检查$ wp_settings_errors数组中是否存在当前页面加载期间声明的任何错误并返回它们。

参数

$setting (string) (Optional) slug标题的特定设置谁是你想要的错误。

$sanitize (boolean) (Optional) 是否在返回错误之前重新清理设置值。

返回值

(array)  设置错误数组

源文件

路径:wp-admin/includes/template.php

<?php
...
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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_settings_errors/