After digging in the setting API I found the answer. Method 1 was correct, but the notification should not be done by hooking to admin_notices
, rather by the function add_settings_error
add_action ('pre_update_option_my_var', function( $new_value, $old_value) {
//Do my validation
$valid = ( 'correct_value' == $new_value );
if ( !$valid ) {
//This fixes the issue
add_settings_error( 'my_var', $error_code, $message );
}
return ($valid)? $new_value : $old_value;
});
In order to display them, this also needs to be added at the beginning of the settings page:
<h2>The heading of my settings page</h2>
<?php settings_errors(); ?>
Related Posts:
- add static page to reading settings for custom post type
- Storing HTML in wp_options
- update_option_$option action not working as expected
- Getting all the settings for a plugin
- Custom setting field value not working inside add_action init hook
- Can I use add_option for a plugin categories and how?
- illegal offset in option add
- Add multiple custom fields to the general settings page
- Settings API vs Theme Customizer
- “Error: Options Page Not Found” on Settings Page Submission for an OOP Plugin
- How to pass arguments from add_settings_field() to the callback function?
- Is get_option function cached?
- How to pass variable to add_settings_section() callback?
- What are the advantages to the Settings API?
- Run function AFTER theme options are saved?
- Where to store plugin settings fields
- WordPress Settings API, Implementing Tabs On Custom Menu Page
- Difference between Option_Group and Option_Name in Register_Settings
- What’s the best method for emptying an option created with the Settings API?
- Settings API – sanitizing urls, email addresses and text
- Settings API – easiest way of validating checkboxes?
- Add your own settings page for plugin
- Settings API – creating reusable form elements?
- Adding an html editor to plugin settings page
- Settings API – changing add_settings_field() output?
- Add settings fields on options discussion admin page
- register_setting() vs add_option()
- Settings API – save an array of options as one setting (array_push?)
- Validate an option array
- How should I use register_setting, add_settings_section, & add_settings_field in my plugin’s options page?
- Custom plugin settings: clicking “save changes” does not display success message
- Dynamic settings fields with Settings API
- Settings API repeater fields
- Setting ‘autoload’ to ‘no’ with Settings API
- Add Settings to Custom Post Type
- do_settings_sections() doesn’t escape quotes
- How to Save Different Settings Options Using Settings API and/or the Theme Optimizer
- Serialized settings in rest api
- Can’t output do_settings_sections . Can’t understand why
- Import / Export Settings API fields values?
- Why won’t register_setting() create a setting?
- Settings API erases itself?
- Saving multiple checkboxes with WordPress settings api
- Settings API – how to update multiple options manually?
- Possible to add another setting to ‘Front page displays’ setting for Custom Post Type
- Setting the uploads directory
- Single callback with multiple setting fields
- Letting user dynamically create fields using Settings API
- Settings API – how to update options manually?
- Cannot update my options using wp_ajax
- how can I detect that option value has changed?
- wordpress settings api add fields
- Adding labels to Setting titles with API
- register_setting sanitize callback $input is null
- Can I dynamically create duplicate fields with the Settings API?
- How to display some settings for super admin user only using Settings API
- Performance of several get_option() calls
- Extend plugin options page
- Settings API – input always updates over validation
- What exactly is the purpose of settings_fields()?
- Can I use the different settings sections over different pages using the save options group?
- Is there a limit to the number of options you can store in one settings field of wp_options?
- Persist fields with Setting API
- wordpress settings API and option array structure
- Echo all API Settings sections?
- Settings Page won’t save
- Settings API get_option best practices
- Custom theme options Radio inputs not saving
- How do I save custom options on the options page ‘Reading’?
- Settings API – generating field value based on a different field?
- Submit options page input values with decimals
- Correct way check nonce (security) using old Options API
- Settings API: Change position of custom setting field
- Using multiple settings_fields() on 1 settings page
- Settings API store serialized data in the database (The Right Way)
- Tab from Settings API resetting all options with INSERT INTO [closed]
- Getting settings saved two times in settings page with tab
- How to get bool with `get_option()` when already registered setting with type `boolean`
- Output ‘do_settings_sections()’ as tabs, not one under the other
- Saving plugin’s settings in 1 field in json format
- get_option array value defaults and best practices
- PHP Notice: Undefined offset: 0 in /wordpress/wp-includes/plugin.php and Register Settings API change
- Hide output of add_settings_field()
- How to make new plugin setting checkbox default as checked
- How to add settings subpage from a plugin to a settings page created in theme?
- How to wrap html around Settings API add_settings_section()
- Remove Internal Style Sheet if no Value Provided?
- Can’t get plugin settings page to save data
- Settings API – sanitize_callback is not called and it leads to an incorrect behavior
- WordPress setting with select – where is my mistake?
- What is the best way to save and retrieve Multiple rows of data with the Settings API?
- Multiple fields with add_settings_field callback
- Settings API: Two update notices after saving settings?
- Settings API in Section
- Storage of array in settings
- Settings API and dynamically generated submenu pages
- The Settings API – What is the purpose of default value in register_setting?
- settings_fields doesn’t appear to be running
- Error: options page not found when I try to save the changes made in my settings page
- Display Each Setting Section In Each Specific Tab within Same Page || Settings API