I'm building a theme using the settings API. I have a top level menu with multiple subpages and everything is working fine, except checkboxes. I am saving the options to a single options array.
This is my sanitation function:
public function sanitize($input) {
$options = get_option(THEMEOPTIONS);
$fields = gosu_adminpanel_fields();
foreach ($fields as $key => $field) {
if($field['callback'] == 'gosu_controls_checkbox') { // if the field is a checkbox
if(!isset($input[$key])) {
$options[$key] = "off";
}
}
}
foreach($input as $key => $field) {
$options[$key] = $value;
}
return $options;
}
$fields is an array with all the field data like type, default values, etc.
Since checkboxes fields are not sent in the POST array when the option is unchecked, it requires the extra validation.
This would work fine if I was using a single menu page, but since I'm using more than one, when I submit one of the pages that doesn't have a checkbox, it sets the value to "off" thinking the user unchecked the box because the field does not exist.
I'd like to be able to validate the checkbox only when the page it is on is submitted or have some other way to make this work without the checkboxes value being set incorrectly.
I want to use only one options array for all the theme's settings; using a variable for each page is out of the question.
I'm considering changing the checkboxes for yes/no dropdowns but that wouldn't really be an elegant solution.