0

I do this widget but the selected options dont saving when updated. This is the code:

/* CUSTOM POSTS LIST */
class Posts_list extends WP_Widget {

    function Posts_list() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'posts_list', 'description' => __('Display differents posts list.', 'fabulous') );
        /* Widget control settings. */
        $control_ops = array( 'width' => 200, 'height' => 350, 'id_base' => 'posts_list' );
        /* Create the widget. */
        $this->WP_Widget( 'posts_list', __('Fabulous posts list', 'fabulous'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );
        /* User-selected settings. */
        $title = apply_filters('widget_title', $instance['title'] );
        $id = $instance['id'];
        $type = $instance['type'];
        $num = $instance['num'];
        $img = $instance['img'];
        $meta = $instance['meta'];

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Title of widget (before and after defined by themes). */
        if ( $title )
            echo $before_title . $title . $after_title; ?>

        <div id="<?php echo $id ?>">
        <?php if ( $type == 'Recent posts' ) {
            echo fab_recent_posts($num, $img, $meta);
        } elseif ($type == 'Popular posts') {
            echo fab_popular_posts($num, $img, $meta);
        } elseif ($type == 'Top rating posts') {
            echo fab_top_rating($num, $img, $meta);
        } ?>
        </div>
        <?php 

        /* After widget (defined by themes). */
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        /* Strip tags (if needed) and update the widget settings. */
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['id'] = $new_instance['id'];
        $instance['type'] = $new_instance['type'];
        $instance['num'] = $new_instance['num'];
        $instance['img'] =  isset( $new_instance['img'] );
        $instance['meta'] =  isset( $new_instance['meta'] );
        return $instance;
    }

    function form( $instance ) {
        /* Set up some default widget settings. */
        $defaults = array( 'title' => '', 'id' =>'posts-list-widget', 'type' => 'recents-posts', 'num' => '5', 'img' => 1, 'meta' => 1);    
        $instance = wp_parse_args( (array) $instance, $defaults ); 
        $type = isset( $new_instance['type'] );
        ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">
<?php _e('Title:', 'fabulous') ?>
</label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'id' ); ?>">
<?php _e('Name:', 'fabulous') ?>
</label>
<input id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" value="<?php echo $instance['id']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('type'); ?>">
<?php _e('Choose posts list type:', 'fabulous'); ?>
</label>
<select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
<?php
$types = array ('Recent posts', 'Top rating posts', 'Popular posts');
foreach ($types as $option) {
echo '<option value="' . $option . '" id="' . $option . '"', $type == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'num' ); ?>">
<?php _e('Number of posts:', 'fabulous') ?>
</label>
<input id="<?php echo $this->get_field_id( 'num' ); ?>" name="<?php echo $this->get_field_name( 'num' ); ?>" value="<?php echo $instance['num']; ?>" style="width:100%;" />
</p>
<p>
  <input class="checkbox" type="checkbox" <?php checked(isset( $instance['img']) ? $instance['img'] : 0  ); ?> id="<?php echo $this->get_field_id( 'showrecents' ); ?>" name="<?php echo $this->get_field_name( 'img' ); ?>" />
  <label for="<?php echo $this->get_field_id( 'img' ); ?>">
<?php _e('Display image', 'fabulous') ?>
</label>
</p>
<p>
  <input class="checkbox" type="checkbox" <?php checked(isset( $instance['meta']) ? $instance['meta'] : 0  ); ?> id="<?php echo $this->get_field_id( 'meta' ); ?>" name="<?php echo $this->get_field_name( 'meta' ); ?>" />
  <label for="<?php echo $this->get_field_id( 'meta' ); ?>">
<?php _e('Display meta', 'fabulous') ?>
</label>
</p>
<?php
    }

}

Nobody can help me please? Thanks!

1
  • "do not saving" is a very brief error description.
    – kaiser
    Commented Oct 15, 2013 at 20:07

1 Answer 1

1

This line $type = isset( $instance['type'] ); will set $type to either true or false which never matches the string you check for later:

foreach ($types as $option) {
   echo '<option value="' . $option . '" id="' . $option . '"', $type == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}

You need to remove that $type = isset( $instance['type'] ); line and use $instance['type'] in the option.

You should also consider using selected().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.