You’d do it something like this:
function sanitize_number_callback ($input){
if( !preg_match( '/...regex for valid here.../', $input ) ){
add_settings_error(
'my_option',
esc_attr( 'my_option' ), //becomes part of id attribute of error message
__( 'Number must be a positive integer', 'wordpress' ), //default text zone
'error'
);
$input = get_option( 'my_option' ); //keep old value
}
return $input;
}
Basically, if validation fails, you return the old saved value. Your validation is completely constraining the input values so no need to sanitize as well.
I don’t know how you’d hold the incorrect value in the form field if validation failed. As far as I can see, the settings API doesn’t allow for it directly. Someone else may be able to provide a solution?
Related Posts:
- “Error: Options Page Not Found” on Settings Page Submission for an OOP Plugin
- In Which Contexts are Plugins Responsible for Data Validation/Sanitization?
- Is get_option function cached?
- What are the advantages to the Settings API?
- Where to store plugin settings fields
- How should one implement add_settings_error on custom menu pages?
- How to properly validate data from $_GET or $_REQUEST using WordPress functions?
- What’s the best method for emptying an option created with the Settings API?
- Why won’t register_setting() create a setting?
- array_map() for sanitizing $_POST
- Performance of several get_option() calls
- Extend plugin options page
- Can I use the different settings sections over different pages using the save options group?
- Correct way check nonce (security) using old Options API
- How to add settings subpage from a plugin to a settings page created in theme?
- Remove Internal Style Sheet if no Value Provided?
- Settings API – sanitize_callback is not called and it leads to an incorrect behavior
- WordPress setting with select – where is my mistake?
- Custom delete option button in plugin settings
- WP_Editor – Saving Value into Plugin Option – Stripping HTML
- Multiple options pages validation for a plugin
- Update problem with update_option() in combination with register_setting()
- Issue on Checkbox with Custom Option Page
- Settings API: Setting default option via ‘get_option’ fails
- How to save Checkbox-Options in Plugin Options Page
- Plugin setting page – update_option problem
- add_settings_error on validating plugin options API
- Array/List Edit in Backend
- How do I build a settings panel under the plugin
- Add_menu_page and saving settings
- Options don’t save, validation callback not executed
- oneOf two possible objects in WP REST API?
- Settings API not Saving to Database or Display
- Admin Message after Plugin Option Updated
- Settings API with arrays example
- How do I add CSS options to my plugin without using inline styles?
- How come `wp_options` table does not have an index on `autoload`?
- Adding Widget form fields dynamically
- Is get_option() faster than accessing get_transient()?
- How to properly sanitize strings for update_option()
- Which to use to execute code during the saving of a plugin settings page?
- Custom plugin settings: clicking “save changes” does not display success message
- How to use the settings API to set multidimensional arrays
- How do I handle multiple Submit buttons in plugin’s option page?
- stray elements
- Deletion of shared options using uninstall.php
- Add Admin Menu Inside Construct or Init
- update_option is not working!
- Save user-specific options in WordPress admin
- How to store accumulate multiple option values in a single array using Options_API
- WordPress Plugin Setting’s POST
- plugin development: storing language specific options
- Using the Settings API, how should I add multiple values to an option?
- Plugin options not being saved or created
- How to sanitize user input?
- What could cause my plugin’s options/settings page not to load?
- How to set “Site Address (URL)” programmatically on WP multisite?
- null callback in add_settings_section
- Using wp_parse_args to set up Plugin Default Settings
- Single sanitization callback for multiple fields
- Callback is not called in add_settings_field() when passed as part of an array, but recognises that it’s there. It’s passed Class to Class using OOP
- When to use add_settings_section vs just register_setting?
- Storing product price data in the database
- How to get specific setting by settings_fields()?
- Parse form values before sending to options.php
- Settings API with arrays example
- Allow users to add / remove settings in plugin
- Show the submitted values in the form when validation fails
- Insert/Update values to a table after blogname option etc. is updated
- wordpress how to query wp_options table
- Plugin Options Array Set to Undefined
- Checked() function on a multidimensional array
- Default settings aren’t used
- Can I use register_settings and unregister_setting once the settings page has loaded?
- WordPress theme options Menu
- Category select options for plugin settings
- Make Database query only when option is updated
- how to save wp_editor html content in options table
- settings api – add_settings_section not working
- Update plugin settings option_name for big plugin update
- What could cause a WP Option to get truncated?
- Decontruct serialized data array from wp_options
- It is possible to pass $args that sent by add_settings_field() inside another function?
- How does one update complex options?
- Settings API not saving
- How do I add a 5 digit ZIP code validation to a Contact7 form?
- $ is not defined [duplicate]
- Hide / show settings field based on other field’s value
- do I need to sanitize a shortcode’s function input?
- Plugins Settings page not updating
- WordPress Boilerplate Plugin doesn’t see callback functions for add_settings_field and add_settings_section
- From my Plugin Settings Page use check boxes to load specific css files (e.g. Bootstrap / Foundation)
- When using an options array the Settings API isn’t creating the database record
- the correct way to use options from settings page [closed]
- How to delete all the options in an option group
- WordPress Plugin default option
- Sanitize and Save metabox values
- How to use register_setting()
- move setting data from wordpress api to codestar freamwork
- esc_url, esc_url_raw or sanitize_url?