Save a checkbox group

  • Unknown's avatar

    Hi,

    I would like to save a checkbox group in a widget but when I save the checkbox get back in the state of unchecked

    Here is the code :

    <?php
    public function form( $instance ) {
    $instance = wp_parse_args( (array) $instance, array('categories' => 0 ) );

    $categories = $instance['categories'];

    $cats = get_categories(array('hide_empty' => false));
    ?>

    <p>
    <?php foreach($cats as $k => $category): ?>
    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('categories') . $k; ?>" name="<?php echo $this->get_field_name('categories'); ?>[]" value="<?php echo $category->cat_ID; ?>" <?php checked(isset($instance['categories']) ? $instance['categories'] : 0); ?>>
    <label for="<?php echo $this->get_field_id('categories') . $k; ?>"><?php echo $category->cat_name; ?></label>

    <?php endforeach; ?>
    </p>
    <?php
    }

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

    $instance['categories'] = $new_instance['categories'];

    return $instance;
    }

    Thank.

    PS : Sorry for my english.

  • Unknown's avatar

    You did not specify a blog address or reason for posting when you created this topic.

    This support forum is for blogs hosted at WordPress.com. If your question is about a self-hosted WordPress blog then you’ll find help at the WordPress.org forums.

    If you don’t understand the difference between WordPress.com and WordPress.org, you may find this information helpful.

    If you forgot to include a link to your blog, you can reply and include it below. It’ll help people to answer your question.

    This is an automated message.

  • The topic ‘Save a checkbox group’ is closed to new replies.