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
- the function do_settings_section($page) generate error “Allowed memory size of … bytes exhausted”
- framework for plugin/theme options panel? [closed]
- 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`?
- Update Option Stored in Multi-Dimensional Array
- Adding Widget form fields dynamically
- Is get_option() faster than accessing get_transient()?
- How to validate custom fields in custom post type?
- How to Use Checkbox in Custom Option Page Using The Setting API
- Accessing plugin settings in gutenberg
- Releasing new plugin version, how to rename old options keys?
- WordPress updates defined vs add_filter?
- What is the difference between esc_html and wp_filter_nohtml_kses?
- Creating Settings Page with dropdowns for Plugin
- How to properly sanitize strings for update_option()
- How to add a new plugin page under desired Options page?
- Which to use to execute code during the saving of a plugin settings page?
- Nonce in settings API with tabbed navigation
- Custom plugin settings: clicking “save changes” does not display success message
- How to use the settings API to set multidimensional arrays
- What is the difference between strip_tags and wp_filter_nohtml_kses?
- 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
- Settings API – Undefined Index when unchecking checkbox
- Disable $title in Plugin Options Screen?
- Multiple entries in get_option results? or why is _multiwidget set to 1?
- WordPress Settings API: saving multiple rows of similar data
- Dropdown list of pages to get page id to store in plugin options
- update_option is not working!
- update_option_{$option} Too Few Arguments
- Save user-specific options in WordPress admin
- How to store accumulate multiple option values in a single array using Options_API
- Why isn’t the Settings API designed to work for plugins using custom admin menus? [duplicate]
- Creating option to allow user to select the page my plugin content will display on
- Unified Approach for Placing Option Pages
- Does settings API create settings on run time?
- WordPress plugin options and jQuery
- WordPress Plugin Setting’s POST
- What is the use of get_option method
- Options API – Validation Callback $input is NULL
- Generating User(s) with Settings API
- Callback function is being called twice
- plugin development: storing language specific options
- Coding a plugin on WordPress; when should I sanitize? [duplicate]
- Best choice of options/settings framework for plugin/theme development
- How to add a secondary button to a settings page with a custom action?
- Tabbed navigation for plugin options using same row in database for all?
- Verify Nonce returns false – Request Nonce returns correct value
- How to store a medium amount of options?
- Settings not set after calling register_setting()
- Using the Settings API, how should I add multiple values to an option?
- GET parameters interfere with my plugin settings
- Plugin settings not saving
- Plugin options not being saved or created
- How to sanitize user input?
- How do i validate data entered in a meta box so that only floats can be entered in a field?
- 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
- Storing HTML in wp_options
- 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