I am writing a plugin and I decided to have multiple pages for different options of the plugin (for clarity’s sake). Doing a single option was a piece of cake, but I’m encountering difficulties when having multiple pages.
All of my options are in an array registered as:
register_setting( 'plugin_options', 'options', 'validator' );
As I said, having only one page was easy. Create the form, submit the form and boom, options saved. Now that I have multiple pages, it seems that WordPress replaces the option array entirely, regardless of the fact that there might be some other values that I don’t want to touch. I tried several options and then googled to finally find this solutions on Otto’s website:
function plugin_options_validate($input) {
$options = get_option('plugin_options');
$options['text_string'] = trim($input['text_string']);
if(!preg_match('/^[a-z0-9]{32}$/i', $options['text_string'])) {
$options['text_string'] = '';
}
return $options;
}
Seems it just won’t work either, and I’m not really sure why.
My actual train of though for a solution is:
- Fetch actual options;
- Run a foreach arrray to change values if I find identical keys in options and input array;
- Return the values and voila!
But I’m hitting a wall. I have many options that are set via checkboxes, and when I uncheck thoses, form won’t submit the name of the checkbox, therefore, I can’t properly go through my array.
I guess what I’m asking is… HOW can I make a callback to register_settings that will take any options of any kind (input, checkbox, etc), and will not destroy the values already saved in the array that aren’t touched by the options I’m saving at that moment.
Any help with this? Thanks !
Ok, after searching a while, I’ve found out that the “easy” solution to my problem would be to start by creating tabs (like in the appearence menu) and option sections. While I’m still no expert in that matter, I followed this 3 part guide that explains pretty much everything related to the matter. I just had to adjust to a plugin, since he wrote his guide having themes in mind !
Thanks to anyone who took time to read !