• Resolved GradyD

    (@adroidman)


    I have a custom widget that I have made but whenever I change the dropdown value it always reverts to the first option when I save the widget. How can I have it display whatever option the user selects?

    // update widget
      function update($new_instance, $old_instance) {
            $instance = $old_instance;
            // Fields
            $instance['category'] = strip_tags($new_instance['category']);
           return $instance;
      }
    
      // widget form creation
      function form($instance) {
        // Check values
        if( $instance) {
             $category = esc_attr($instance['app_category']);
             $instance['category'] = strip_tags( $new_instance['category'] );
        } else {
             $category = '';
        }
        //Set up some default widget settings.
        $defaults = array( 'category' => '');
        $instance = wp_parse_args( (array) $instance, $defaults );
    ?>
        <!-- Category Select Menu -->
        <p>
          <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('App Category: ', 'AppManager_plugin'); ?></label>
          <h1><?php echo $category ?></h1>
          <h1><?php echo $instance['category'] ?></h1>
          <select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('Category'); ?>" >
            <?php foreach(get_terms('app_category','parent=0&hide_empty=0') as $term) { ?>
              <option <?php selected( $instance['category'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
            <?php } ?>
          </select>
        </p>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try changing the form() function to omething similar to this [untested]:

    function form( $instance ) {
    	// Check values
    	$category = isset( $instance['category'] ) ? (int) $instance['category'] : '';
    	$terms = get_terms( 'app_category', 'parent=0&hide_empty=0' );
    	if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
    ?>
        <!-- Category Select Menu -->
        <p>
          <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'App Category: ', 'AppManager_plugin' ); ?></label>
          <h1><?php echo $category ?></h1>
          <h1><?php echo $instance['category'] ?></h1>
          <select id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'Category' ); ?>" >
            <?php foreach ( $terms as $term ) { ?>
              <option <?php selected( $category, $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
            <?php } ?>
          </select>
        </p>
    <?php
    	}
    }
    ?>

    Thread Starter GradyD

    (@adroidman)

    Sadly I was not able to get it working using your code.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try seeing what the instance holds when updating the widget by adding this to the top of the form function:

    echo '<pre>';
    print_r($instance);
    echo '</pre>';

    Moderator keesiemeijer

    (@keesiemeijer)

    Also try changing this:

    name="<?php echo $this->get_field_name( 'Category' ); ?>"

    to this

    name="<?php echo $this->get_field_name( 'category' ); ?>"

    Thread Starter GradyD

    (@adroidman)

    That did it!! Changing the ‘C’ to a lowercase ‘c’ fixed the issue. Thank you!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Widget dropdown always displays first option’ is closed to new replies.