Results 1 to 4 of 4

Thread: Dropdown options in widget control panel don't persist

  1. #1
    rickandersonaia's Avatar
    rickandersonaia is offline Hello World
    Join Date
    Feb 2010
    Location
    Seattle, WA
    Posts
    1

    Default Dropdown options in widget control panel don't persist

    So I'm working on my first plugin which allows the user to select from a dropdown list in the widget control panel.

    The widget works fine in that the option is executed. Unfortunately the option they chose no longer shows in the control panel it reverts back to the default option.

    I'm using the 2.8 API & I never learned the previous method.

    Any suggestion on how I'd fix this?

    Here's what I think are the relevant sections of the code:
    PHP Code:
    /**
         * How to display the widget on the screen.
         */

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

            
    $style $instance['style']; 
    PHP Code:
    /**
         * Update the widget settings.
         */

        
    function update$new_instance$old_instance ) {
            
    $instance $old_instance;
            
            
    $instance['style'] = $new_instance['style'];
                    
            return 
    $instance;
        } 
    PHP Code:
    function form( $instance ) {
            
            /* Set up some default widget settings. */
            $defaults = array( 'title' => '', 'image' => '', 'link' => '',    'link_text' => '',    'id' =>'',    'style' => '');
            $instance = wp_parse_args( (array) $instance, $defaults ); ?>

            
            <!-- Style: Select Box -->
            <p>
                <label for="<?php echo $this->get_field_id'style' ); ?>"><?php _e('style:'''); ?></label> 
                <select id="<?php echo $this->get_field_id'style' ); ?>" name="<?php echo $this->get_field_name'style' ); ?>" class="widefat" style="width:100%;">
                    <option <?php if ( 'standard' == $instance['style'] ) echo 'selected="selected"'?>>Standard-Box</option>
                    <option <?php if ( 'box2' == $instance['style'] ) echo 'selected="selected"'?>>Box-Type-2</option>
                    <option <?php if ( 'box3' == $instance['style'] ) echo 'selected="selected"'?>>Box-Type-3</option>
                    <option <?php if ( 'box4' == $instance['style'] ) echo 'selected="selected"'?>>Box-Type-4</option>
                    <option <?php if ( 'box5' == $instance['style'] ) echo 'selected="selected"'?>>Box-Type-5</option>
                    <option <?php if ( 'box6' == $instance['style'] ) echo 'selected="selected"'?>>Box-Type-6</option>
                </select>
            </p>

        <?php
        
    }

  2. #2
    MRDPE is offline Hello World
    Join Date
    Feb 2010
    Posts
    2

    Default

    OK - so I have had the same problem - and it has puzzled me for a couple of days now. Eventually I figured out the problem was the "option" line in the select box:

    PHP Code:
    <option <?php if ( 'box6' == $instance['style'] ) echo 'selected="selected"'?>>Box-Type-6</option>
    The "if" statement was not working for whatever reason - so "selected" was never being echoed.

    My solution - and I can't really explain why this works - was to break this one line into three (here is the code from my new widget):

    PHP Code:
      echo '<option ';
      if (
    $instance["country"] == 'US') {echo 'selected="selected"';} 
      echo 
    ' value="US">United States</option>';
     
      echo 
    '<option ';
            if (
    $instance["country"] == 'GB') {echo 'selected="selected"';} 
      echo 
    ' value="GB">United Kingdom</option>';
     
      echo 
    '<option ';
            if (
    $instance["country"] == "CA") {echo 'selected="selected"';} 
      echo 
    ' value="CA">Canada</option>'
    I hope this helps.

    MRDPE
    http://TheProfessionalEngineer.com

  3. #3
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Ugh. Big repetitive if blocks like that will give you cancer.

    The WordPress way:

    PHP Code:
    // php code
    ?>
    <option value="US" <?php selected('US'$instance["country"]); ?>>United States</option>
    <option value="GB" <?php selected('GB'$instance["country"]); ?>>United Kingdom</option>
    <option value="CA" <?php selected('CA'$instance["country"]); ?>>Canada</option>
    <?php
    // more code
    Got checkboxes? Try the checked() function instead of selected().

  4. #4
    MRDPE is offline Hello World
    Join Date
    Feb 2010
    Posts
    2

    Icon14

    Thanks - this is great.

    I really didn't like those "if blocks".
    I tried the code and it works fine.

    Thanks again.

    MRDPE
    http://TheProfessionalEngineer.com

Posting Permissions

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