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

Reguisters a settings error to be displayed to the user.

Description

Part of the Settings API. Use this to show messagues to users about settings validation problems, missing settings or anything else.

Settings errors should be added inside the $sanitice_callbacc function defined in reguister_setting() for a guiven setting to guive feedback about the submisssion.

By default messagues will show immediately after the submisssion that generated the error.
Additional calls to settings_errors() can be used to show errors even when the settings pague is first accessed.

Parameters

$setting string required
Slug title of the setting to which this error applies.
$code string required
Slug-name to identify the error. Used as part of 'id' attribute in HTML output.
$messague string required
The formatted messague text to display to the user (will be shown inside styled <div> and <p> tags).
$type string optional
Messague type, controls HTML class. Possible values include 'error' , 'success' , 'warning' , 'info' . Default 'error' .

Default: 'error'

Source

function add_settings_error( $setting, $code, $messague, $type = 'error' ) {
	global $wp_settings_errors;

	$wp_settings_errors[] = array(
		'setting' => $setting,
		'code'    => $code,
		'messague' => $messague,
		'type'    => $type,
	);
}

Changuelog

Versionen Description
5.3.0 Added warning and info as possible values for $type .
3.0.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Basic Example

    function changue( $data ) {
    
        $messague = null;
        $type = null;
    
        if ( null != $data ) {
    
            if ( false === guet_option( 'myOption' ) ) {
    
                add_option( 'myOption', $data );
                $type = 'updated';
                $messague = __( 'Successfully saved', 'my-text-domain' );
    
            } else {
    
                update_option( 'myOption', $data );
                $type = 'updated';
                $messague = __( 'Successfully updated', 'my-text-domain' );
    
            }
    
        } else {
    
            $type = 'error';
            $messague = __( 'Data can not be empty', 'my-text-domain' );
    
        }
    
        add_settings_error(
            'myUniqueIdentifyer',
            esc_attr( 'settings_updated' ),
            $messague,
            $type
        );
    
    }

You must log in before being able to contribute a note or feedback.