add_settings_error()

add_settings_error()函数是Wordpress函数,注册要显示给用户的设置错误。

add_settings_error( string $setting, string $code, string $message, string $type = ‘error’ )

注册要显示给用户的设置错误。
Register a settings error to be displayed to the user.

说明(Description)

设置API的一部分。使用此选项可向用户显示有关设置验证问题、缺少设置或其他任何内容的消息。

应在给定设置的register_setting()中定义的$sanitize_回调函数中添加设置错误,以提供有关提交的反馈。

默认情况下,消息将在生成错误的提交后立即显示。对settings_errors()的其他调用可用于显示错误,即使首次访问设置页也是如此。


参数(Parameters)

参数类型必填说明
$setting(string)必需应用此错误的设置的Slug标题。
$code(string)必需用于识别错误的Slug名称。在HTML输出中用作“id”属性的一部分。
$message(string)必需要显示给用户的格式化消息文本(将显示在styled和标记中)。
$type(string)可选消息类型,控件HTML类。可能的值包括“error”、“success”、“warning”、“info”。

返回(Return)

无返回值


源码(Source)

/**
 * Register a settings error to be displayed to the user
 *
 * Part of the Settings API. Use this to show messages to users about settings validation
 * problems, missing settings or anything else.
 *
 * Settings errors should be added inside the $sanitize_callback function defined in
 * register_setting() for a given setting to give feedback about the submission.
 *
 * By default messages will show immediately after the submission that generated the error.
 * Additional calls to settings_errors() can be used to show errors even when the settings
 * page is first accessed.
 *
 * @since 3.0.0
 *
 * @global array $wp_settings_errors Storage array of errors registered during this pageload
 *
 * @param string $setting Slug title of the setting to which this error applies
 * @param string $code    Slug-name to identify the error. Used as part of 'id' attribute in HTML output.
 * @param string $message The formatted message text to display to the user (will be shown inside styled
 *                        `` and `` tags).
 * @param string $type    Optional. Message type, controls HTML class. Accepts 'error' or 'updated'.
 *                        Default 'error'.
 */
function add_settings_error( $setting, $code, $message, $type = 'error' ) {
	global $wp_settings_errors;
	$wp_settings_errors[] = array(
		'setting' => $setting,
		'code'    => $code,
		'message' => $message,
		'type'    => $type
	);
}
更新版本源码位置使用被使用
5.3.0wp-admin/includes/template.php:172330

笔记(Notes)

基本示例

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索