Title: add_settings_error
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#wp--skip-link--target)

Registers a settings error to be displayed to the user.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#description)󠁿

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()](https://developer.wordpress.org/reference/functions/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()](https://developer.wordpress.org/reference/functions/settings_errors/)
can be used to show errors even when the settings page is first accessed.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#parameters)󠁿

 `$setting`stringrequired

Slug title of the setting to which this error applies.

`$code`stringrequired

Slug-name to identify the error. Used as part of `'id'` attribute in HTML output.

`$message`stringrequired

The formatted message text to display to the user (will be shown inside styled `
<div>` and `<p>` tags).

`$type`stringoptional

Message type, controls HTML class. Possible values include `'error'`, `'success'`,`'
warning'`, `'info'`. Default `'error'`.

Default:`'error'`

## 󠀁[Source](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#source)󠁿

    ```php
    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,
    	);
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-admin/includes/template.php#L1868)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-admin/includes/template.php#L1868-L1877)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Privacy_Requests_Table::process_bulk_action()](https://developer.wordpress.org/reference/classes/wp_privacy_requests_table/process_bulk_action/)`wp-admin/includes/class-wp-privacy-requests-table.php` |

Process bulk actions.

  | 
| [_wp_personal_data_handle_actions()](https://developer.wordpress.org/reference/functions/_wp_personal_data_handle_actions/)`wp-admin/includes/privacy-tools.php` |

Handle list table actions.

  | 
| [sanitize_option()](https://developer.wordpress.org/reference/functions/sanitize_option/)`wp-includes/formatting.php` |

Sanitizes various option values based on the nature of the option.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.3.0](https://developer.wordpress.org/reference/since/5.3.0/) | Added `warning` and `info` as possible values for `$type`. | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/add_settings_error/?output_format=md#comment-content-1125)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/add_settings_error/#comment-1125)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_settings_error%2F%23comment-1125)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_settings_error%2F%23comment-1125)
 4. **Basic Example**
 5.     ```php
        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
            );
    
        }
        ```
    
 6.  * You must return the $data otherwise it won’t show in the field. [PHP] 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 ); return $data; } [/php]
     * [I am Programmer](https://profiles.wordpress.org/iamprogrammerlk/) [5 years ago](https://developer.wordpress.org/reference/functions/add_settings_error/#comment-5183)
     * You must return the $data otherwise it won’t show in the field.
     * [I am Programmer](https://profiles.wordpress.org/iamprogrammerlk/) [5 years ago](https://developer.wordpress.org/reference/functions/add_settings_error/#comment-5184)
 7.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_settings_error%2F%3Freplytocom%3D1125%23feedback-editor-1125)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_settings_error%2F)
before being able to contribute a note or feedback.