add_settings_error

Register a settings error to be displayed to the user.

Description

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

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 errors" title="settings_errors" class="code function">settings_errors() can be used to show errors even when the settings page is first accessed.

Parameters (4)

0. $setting (string)
Slug title of the setting to which this error applies
1. $code (string)
Slug-name to identify the error. Used as part of id attribute in HTML output.
2. $message (string)
The formatted message text to display to the user (will be shown inside styled
and

tags).

3. $type — Optional. (string) => 'error'
Message type, controls HTML class. Accepts error or updated.. Default error .

Usage

  1. if ( !function_exists( 'add_settings_error' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  3.  
  4. // Slug title of the setting to which this error applies 
  5. $setting = ''; 
  6.  
  7. // Slug-name to identify the error. Used as part of 'id' attribute in HTML output. 
  8. $code = ''; 
  9.  
  10. // The formatted message text to display to the user (will be shown inside styled 
  11. // `
    ` and `

    ` tags). 

  12. $message = ''; 
  13.  
  14. // Optional. Message type, controls HTML class. Accepts 'error' or 'updated'. 
  15. // Default 'error'. 
  16. $type = 'error'; 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = add_settings_error($setting, $code, $message, $type); 
  20.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/template.php  
  1. function add_settings_error( $setting, $code, $message, $type = error ) { 
  2. global $wp_settings_errors; 
  3.  
  4. $wp_settings_errors[] = array( 
  5. 'setting' => $setting,  
  6. 'code' => $code,  
  7. 'message' => $message,  
  8. 'type' => $type 
  9. );