Settings API – Checkboxes validation in a menu with multiple subpages

Since I’m getting no answers, here’s the workaround I ended up using in case anyone runs into the same issue.

I added a hidden field for each checkbox with the key as the value, then in the sanitisation function I simply check for the hidden field instead of the checkbox so I can tell wether or not a checkbox belong to a page. Here’s the final code:

public function merge_options($input) {

    $options = get_option(THEMEOPTIONS);
    $fields = gosu_adminpanel_fields();

    if(!empty($_POST)):
    foreach($_POST['checkbox'] as $key) {
        if($fields[$key]['callback'] == 'gosu_controls_checkbox') {

            if(!isset($input[$key])) {
                $options[$key] = "off";
            }

        }
    }
    endif;

    foreach($input as $key => $value) {
        $options[$key] = $value;
    }

    return $options;

}

You need to check the POST array because the array that is sent as an argument to the callback function clean every non-registered variable and it would really be unnecessary to add the fields as a setting (might be a bit unsafe but it works).