Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
add_settings_error() - 注册要向用户显示的设置错误信息 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / add_settings_error()

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

注册要向用户显示的设置错误信息


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 3 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "d369e5096771b4ef95be960ff7c1c3db" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "d1d06e862a22177240489c9695b89fcc" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "f0fe3897ddf9e887b9d6c1994bd1943c" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

addmore...

error

settings 设置more...


描述

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

应该在register_setting()中定义的$sanitize_callback函数内为给定的设置添加设置错误信息,以反馈提交的情况。

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


参数

$setting

(string)(必填) 应用此错误的设置的slug标题。

$code

(string)(必填) 用于识别错误的slug名称。用作HTML输出中'id'属性的一部分。

$message

(string)(必填) 要向用户显示的格式化消息文本(将显示在样式化的<div>和<p>标签中)。

$type

(string)(可选) 消息类型,它控制HTML类。可能的值包括'error'、'success'、'warning'、'info'。

默认值: 'error'



源码

查看源码 官方文档


更新日志

版本描述
5.3.0添加了warning和info作为$type的可用值。
3.0.0开始引入

使用示例

  • 示例1

    基本示例

    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
        );
    
    }