WordPress函数文档add_settings_error()
注册添加一个设置错误提示信息 描述 Register a settings error to be displ…
注册添加一个设置错误提示信息
描述
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.
用法
<?php add_settings_error( $setting, $code, $message, $type ) ?>
参数
$setting
(string) (必填) Slug title of the setting to which this error applies.
默认值: None
$code
(string) (必填) Slug-name to identify the error. Used as part of ‘id’ attribute in HTML output. A prefix of ‘setting-error-‘ will be added to the string in $code and assigned to the ‘id’ attribute of the outermost <div> for this error.
默认值: None
$message
(string) (必填) The formatted message text to display to the user (will be shown inside styled <div> and <p>).
默认值: None
$type
(string) (可选) The type of message it is. $type will be add an HTML class of the same name of the outermost <div>. To add multiple classes separate them with a space.
- error
- updated
默认值: ‘error’
返回值
(void)
This function does not return a value.
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function change( $data ) {
$message = null;
$type = null;
if ( null != $data ) {
if ( false === get_option( ‘myOption’ ) ) {
add_option( ‘myOption’, $data );
$type = ‘updated’;
$message = __( ‘Successfully saved’, ‘my-text-domain’ );
} else {
update_option( ‘myOption’, $data );
$type = ‘updated’;
$message = __( ‘Successfully updated’, ‘my-text-domain’ );
}
} else {
$type = ‘error’;
$message = __( ‘Data can not be empty’, ‘my-text-domain’ );
}
add_settings_error(
‘myUniqueIdentifyer’,
esc_attr( ‘settings_updated’ ),
$message,
$type
);
}
|
注意
The problem only occurs when I use add_menu_page() or add_submenu_page(). The validation errors are not shown.
A simple work around is to add settings_errors() to my options page, which will make the notification box show… However, seems like a dirty fix
1
2
3
4
5
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<h2>The heading of my settings page</h2>
<?php settings_errors(); ?>
|
历史
添加于 版本: 3.0
源文件
add_settings_error() 函数的代码位于 wp-admin/includes/template.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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 `<p>` 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
);
}
|
相关
Settings API:
register_setting(),
unregister_setting(),
add_settings_field(),
add_settings_section(),
add_settings_error(),
get_settings_errors(),
settings_errors()
- 原文:http://codex.wordpress.org/Function_Reference/add_settings_error
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!