WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So, i successfully managed to add a drop down option, the problem is when i click save all of the options are removed, however the chosen option is implemented successfully.

Here is the relevant code:

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

        $pfxattribution = $instance['pfx-attribution'];

            <label for="<?php echo $this->get_field_id('pfx-attribution'); ?>"><?php _e('Choose attribution:'); ?></label>
            <select id="<?php echo $this->get_field_id('pfx-attribution'); ?>" name="<?php echo $this->get_field_name('pfx-attribution'); ?>" class="widefat" /> 
            <option>Foreign Exchange</option>
            <option>Currency Exchange</option>

function update($new_instance, $old_instance) {
        // Get the old values
        $instance = $old_instance;

        // Update with any new values (and sanitise input)
        $instance['pfx-attribution'] = strip_tags( $new_instance['pfx-attribution'] );

       return $instance;

Any help is greatly appreciated

share|improve this question
up vote 4 down vote accepted

Danny, this is pretty much lifted straight from a plug-in I've made:

 <?php function form($instance){    
    $instance = wp_parse_args( (array) $instance, $this->w_arg );
    <select id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" type="text">
        <option value="asc" <?php selected($instance['order'], 'asc'); ?>>ASC </option>
        <option value="desc" <?php selected($instance['order'], 'desc');?>>DESC </option>


Without seeing the rest of your code I can't be sure the root of the problem, but it could be the form is not wrapped inside the form function. If the above doesn't seem to work for you, perhaps pastie your Widget Class?

I should perhaps add that my widget class has a variable w_arg, an array of defaults! E.g.

var $w_arg = array(
        'order'=> 'ASC'
share|improve this answer
Thanks for your input Stephen, it is wrapped correctly inside the form function, I also have a bunch of other plain text box options which are working flawlessly, I also have defaults defined in a very similar way to you. The problem appears to be specifically related to drop down options and check boxes! – Danny Jan 23 '12 at 12:58

Your <option>s need value= or they do not send any value...essentially right now value='', which is, I'd bet, what you're seeing.

share|improve this answer
Thanks for the reply, everything works perfectly until I hit save then all options disappear, sadly adding a 'value' to my options didn't make any difference. :-( – Danny Jan 23 '12 at 10:58
Well you also lack selected() calls...it should be in the DB now, you're just not pulling it when you load the box – mor7ifer Jan 23 '12 at 11:19
Without a value attribute, an option will send its contents (w3 ref). – Geert Jan 23 '12 at 11:23
Thanks for clearing that up Geert. Thanks again mOr7if3r - I'm not familiar with selected() calls, I'll look it up now and see if that fixes anything. Thanks for your help. – Danny Jan 23 '12 at 11:30

Your <select> should not have a value attribute, nor the type="text" attribute.

share|improve this answer
Thanks Geert, very sloppy on my part! Sadly the options still disappear when i hit save... – Danny Jan 23 '12 at 11:35
Well, time to debug further then. Do a var_dump() of $_POST or $new_instance. – Geert Jan 23 '12 at 12:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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