So, I am working on a certain widget, and want to present an option as a checkbox that returns a value of "true" if checked, and "false" if unchecked, for a defined variable $option:
Here's what I have for the form input:Code:$option = $instance['option'];
I have (attempted to) set the default value for $option to true (condensing from the full array):Code:<input class="checkbox" type="checkbox" <?php checked( $instance['option'], true ); ?> id="<?php echo $this->get_field_id( 'option' ); ?>" name="<?php echo $this->get_field_name( 'option' ); ?>" /> <label for="<?php echo $this->get_field_id( 'option' ); ?>">Enable Option?</label>
However, when the widget is instantiated, the checkbox is unchecked.Code:$defaults = array( 'option' => 'true'); $instance = wp_parse_args( (array) $instance, $defaults );
I can check the checkbox, but when I save, the checkbox reverts to being unchecked.
Here is the relevant part of the update code (note: all my other, non-checkbox inputs are updating properly):
Is there any particular reason this isn't working?Code:$instance['option'] = $new_instance['option'];
I suppose I need somehow to echo checked="checked", if the checkbox is checked. Not sure how to do that. EDIT: That's what the checked() function is supposed to do - but it is, apparently, returning false.
Googling turns up several examples of checkboxes, but they all seem to be widgets that have options arrays (or something), and mine is more simple.
Can anyone help?
Thanks in advance!


LinkBack URL
About LinkBacks



Reply With Quote
