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.

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!

 add_settings_field('the_option_label',
                    'Display Paragraph:',
                     array($this,'the_option_label_setting'),
                     __FILE__,
                     'hte_main_section');

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

Thanks

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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 '13 at 5:41
1  
Edited Till's answer with corrected code from the article. –  Charles Clarkson Aug 18 '13 at 6:12
    
Thanks Charles, –  Mona Coder Aug 18 '13 at 6:30

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.