get_settings_errors()
Fetch settings errors registered by add_settings_error()
Checks the $wp_settings_errors array for any errors declared during the current pageload and returns them.
If changes were just submitted ($_GET['settings-updated']) and settings errors were saved to the 'settings_errors' transient then those errors will be returned instead. This is used to pass errors back across pageloads.
Use the $sanitize argument to manually re-sanitize the option before returning errors. This is useful if you have errors or notices you want to show even when the user hasn't submitted data (i.e. when they first load an options page, or in the admin_notices action hook).
No Hooks.
Return
Array[]
. Array of settings error arrays.
Usage
get_settings_errors( $setting, $sanitize );
- $setting(string)
- Slug title of a specific setting whose errors you want.
Default: '' - $sanitize(true|false)
- Whether to re-sanitize the setting value before returning errors.
Default: false
Examples
#1 An example of how the function works
Display the messages for the "my_option_name" option that were registered when trying to save the option. If the value of the option failed, the error messages were registered by the add_settings_error() function, when saving the options. After the form data has been sent, we can get these messages with get_settings_errors() function and output them with admin_notices hook. To do that, we write our own function which processes the result of get_settings_errors() and outputs all the messages on the screen:
add_action( 'admin_notices', 'your_admin_notices_action' ); function your_admin_notices_action() { $settings_errors = get_settings_errors( 'my_option_name' ); if ( empty( $settings_errors ) ){ return; } $output = ''; foreach ( $settings_errors as $key => $details ) { $css_id = 'setting-error-' . $details['code']; $css_class = $details['type'] . ' settings-error'; $output .= "<div id='$css_id' class='$css_class'> n"; $output .= "<p><strong>{$details['message']}</strong></p>"; $output .= "</div> n"; } echo $output; }
This is just a demonstration of how the function works. Registered messages are automatically output by WordPress when you save data, you just need to check the data before saving and register messages with add_settings_error() if necessary. Separately displaying messages, as in this example, is usually not necessary.
Notes
- Global. Array[]. $wp_settings_errors Storage array of errors registered during this pageload
Changelog
Since 3.0.0 | Introduced. |