Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.