Results 1 to 5 of 5

Thread: Widget Options - Using Checkboxes

  1. #1
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default Widget Options - Using Checkboxes

    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:

    Code:
    $option = $instance['option'];
    Here's what I have for the form input:

    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>
    I have (attempted to) set the default value for $option to true (condensing from the full array):

    Code:
    $defaults = array( 'option' => 'true');
            $instance = wp_parse_args( (array) $instance, $defaults );
    However, when the widget is instantiated, the checkbox is unchecked.

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

    Code:
    $instance['option'] = $new_instance['option'];
    Is there any particular reason this isn't working?

    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!
    Last edited by chipbennett; 12-22-2009 at 04:12 PM.
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  2. #2
    greenshady's Avatar
    greenshady is offline Here For The Peanuts
    Join Date
    Jan 2009
    Posts
    170

    Default

    In your update() method, you can make sure it's saved like so:

    PHP Code:
    $instance['option'] = ( isset( $new_instance['option'] ) ? ); 

  3. #3
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default

    After poking through the defauld widgets, I've made the following changes:

    Defining the variable:

    Code:
    $option = isset($instance['option']) ? $instance['option'] : true;
    Update function:

    Code:
    $instance = array( 'option' => 0, 'option2' => 0);
      foreach ( $instance as $field => $val ) {
       if ( isset($new_instance[$field]) )
        $instance[$field] = 1;
      }
    These work to allow the checkbox to be checked/unchecked, and that value retained.

    Now, why is my default setting (checked) not applying?

    I assume that it's because the checked() function is returning false - but why?
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  4. #4
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default

    Just FYI: I got it all working.

    Requested to add my first plugin (widget) to the wordpress.org plugin repository!
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  5. #5
    Cais's Avatar
    Cais is offline Big Tipper
    Join Date
    Feb 2009
    Location
    Mississauga, ON, CANADA
    Posts
    349

    Default

    Quote Originally Posted by chipbennett View Post
    ...
    Here's what I have for the form input:
    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>
    ...
    Can anyone help?
    Maybe after the fact, but I found this information useful in a few plugins ...
    http://wpfirstaid.com/2009/12/keep-c...k-in-checkbox/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •