How to Use Checkbox in Custom Option Page Using The Setting API

Have a look at: The Complete Guide To The WordPress Settings API (Part 8: Validation, Sanitisation, and Input II):

add_settings_field(  
    'Checkbox Element',  
    'Checkbox Element',  
    'sandbox_checkbox_element_callback',  
    'sandbox_theme_input_examples',  
    'input_examples_section'  
);

function sandbox_checkbox_element_callback() {

    $options = get_option( 'sandbox_theme_input_examples' );

    $html="<input type="checkbox" id="checkbox_example" name="sandbox_theme_input_examples[checkbox_example]" value="1"" . checked( 1, $options['checkbox_example'], false ) . '/>';
    $html .= '<label for="checkbox_example">This is an example of a checkbox</label>';

    echo $html;

}

EDIT: Check box fields do not use the value attribute to determine if the box is checked. They use a checked attribute. The $html line above has been edited using the code from later in the article referenced above. Read the articles for details.

Leave a Comment