Languages: English • 日本語 (Add your language)
Register a settings error to be displayed to the user.
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 settings_errors() can be used to show errors even when the settings page is first accessed.
<?php add_settings_error( $setting, $code, $message, $type ) ?>
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( 'myUniqueIdentifier', esc_attr( 'settings_updated' ), $message, $type ); }
The problem only occurs when I use add_menu_page() or add_submenu_page(). The validation errors are not shown.
A simple work around is to add settings_errors() to my options page, which will make the notification box show... However, seems like a dirty fix
<h2>The heading of my settings page</h2> <?php settings_errors(); ?>
Since: 3.0
add_settings_error() is located in wp-admin/includes/template.php
.
Settings API: register_setting(), unregister_setting(), add_settings_field(), add_settings_section(), add_settings_error(), get_settings_errors(), settings_errors()