Saving checkbox value in wordpress widget issue

I created a custom Flickr Widget for WordPress and successfully set up an options form for users to enter their Flickr information, but I cannot get the checkbox in the form to save whether or not it is checked. Any help would be greatly appreciated! Here are my widget(), form(), and update() functions:

function widget($args, $instance) {
    extract($args);

    $title = apply_filters('widget_title', $instance['title']);
    $displaynum = $instance['displaynum'];
    $flickrid = $instance['flickrname'];
    $num = 6;
    $feed = new SimplePie($instance['feed']);
    $feed->handle_content_type(); 
    $photostream = $instance['show_photostream'];


function update($new_instance, $old_instance) {
    $instance = $old_instance;

    $instance['title'] = strip_tags($new_instance['title'] );
    $instance['displaynum'] = strip_tags($new_instance['displaynum'] );
    $instance['feed'] = $new_instance['feed'];
    $instance['flickrname'] = $new_instance['flickrname'];
    $instance['show_photostream'] = (bool) $new_instance['show_photostream'];

    return $instance;
}

function form($instance) {
    $defaults = array (
        'title' => 'My Recent Flickr Uploads',
        'displaynum' => 6,
        'feed' => 'http://api.flickr.com/services/feeds/photos_public.gne?id=33927859@N06&lang=en-us&format=rss_200',
        'flickrname' => 'rastajellyfish',
        'show_photostream' => isset( $instance['show_photostream'] ) ? (bool) $instance['show_photostream'] : false
    );
    $instance = wp_parse_args( (array) $instance, $defaults); ?>

Any help would be greatly appreciated!!!

Related posts

Leave a Reply

3 comments

  1. 5 minutes ago, I ran into the same problem as you with a checkbox in my custom image widget, and looking through the default widgets as Hnrch suggested solved it.

    My problem was caused by echoing a wrong name attribute on my checkbox input. There’s a method called get_field_name() that’ll output the correct input name. Here’s an example of my (now functional) checkbox:

    <input id="<?php echo $this->get_field_id('linktarget'); ?>" name="<?php echo $this->get_field_name('linktarget'); ?>" type="checkbox" <?php checked(isset($instance['linktarget']) ? $instance['linktarget'] : 0); ?> />&nbsp;
    

    ‘linktarget’ should be ‘show_photostream’ in your case

  2. Not exactly what you are looking for but this is how I saved a checkbox in metabox, you may get some hint from this…

    Code used to display html

    function html_rtsocial_metabox()
    {
      global $post;
    
      $custom = get_post_custom($post->ID);
      $suppress = $custom['_suppress_rtsocial'][0];
      $checked = ($suppress=='yes')?'checked':'';
      $value   = ($suppress=='yes')?'yes':'no';
      $html  = '<br><input type="checkbox" name="_suppress_rtsocial" value="'.$value.'" '.$checked.' id="_suppress_rtsocial" /> Hide Social Icons ???';
      $html .= '<br/><br/>';
      echo $html;
    }
    

    While saving

    update_post_meta($post_id,'_suppress_rtsocial',$_POST['_suppress_rtsocial']);
    

    Added js for admin interface

    function checkbox_helper(){
      var ele =jQuery('#_suppress_rtsocial'),value;
      ele.click(function(){
                  value = ele.is(':checked')?'yes':'no';
                  ele.val(value);
                }
    );
    }