Can you please let me know how I can add a checkbox to Setting API in creating Custom Options Page? I am using the following code to add a txt box which is working perfectly but I am totally confused how to add chechbox to the setting API and options. I just tried ti update the input type to checkbox but I am not sure what to put in Value besides when I save the form the check box still appears unchecked!

                    'Display Paragraph:',

 public function the_option_label_setting()
       echo "<input name='My_Theme_Options[the_option_label]' type='text' value='{$this->options['the_option_label']}'/>";



1 Answer 1


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

    'Checkbox Element',  
    'Checkbox Element',  

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.

  • Hi Till Thanks for your comment I tried your hint and the code from the link but I am still having some issues here! The problem is when I checked the checkbox and save the options the new page is still unchecked! I mean it is not reading the data from the Options! Can you please let me know how to do this ? I know in Text box we can use the value='{$this->options['the_option_label']} but how about the checkbox?
    – Mona Coder
    Aug 18, 2013 at 5:41
  • 1
    Edited Till's answer with corrected code from the article. Aug 18, 2013 at 6:12
  • Maybe it's better if we check the option is set like checked( 1, $options['checkbox_example'] ?? '', false ). Just in case we don't want an error or a warning message for that. Jul 25, 2023 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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