Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok, I'm developing my own widget and I've a HUGE problem.

I don't know how to fetch and eventually save html selects value.

Simple example:

/* initialize TITLE */
function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters('widget_title', $instance['title'] );

(...)

/* update code for TITLE */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );

(...)

/* creating TITLE field in widgets admin area */

 <p>
    <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
    <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" type="text" style="width:100%;" />
 </p>

(...)

OK. Now when I type

<?php echo $instance['title'] ?>

It's going to show my title!

But I have this list:

<label for="<?php echo $this->get_field_id( 'example' ); ?> "><?php _e('Map type:', 'example'); ?></label>
<select id="<?php echo $this->get_field_id( 'example' ); ?>" name="<?php echo $this->get_field_id( 'example' ); ?>">
     <option value="supervalue" selected="selected">Super Value</option>
</select>

And

<?php echo $instance['example'] ?>

Gives NOTHING. I've been looking everywhere including default-widgets.php but I don't understand a word since all default plugins use mostly dynamically generated option lists.

I've used only one item list, but in fact it's longer, I'm not sure how to save the selected element also, since I don't know how to access this element's value.

Any ideas? I spent the whole day on this, getting totally mad slowly.

[edited]

I didn't show you the update function because as I mentioned above I use the same update code for each function.

 function update( $new_instance, $old_instance ) {

    $instance = $old_instance;
    $instance['example'] = strip_tags( $new_instance['example'] );    

    return $instance;

}
share|improve this question
    
i assuming that you are using a class decleration of the widget so post your update function and i think i know whats the problem. –  Bainternet Feb 20 '11 at 23:47
    
Dunno if this matters, but shouldn't it be name="<?php echo $this->get_field_name( 'example' );? You're using get_field_id() above. –  wyrfel Feb 21 '11 at 0:57
    
Why isn't the new field inside the update routine? Eg. $instance['example'] = your_chosen_sanitization_function( $new_instance['example'] ); –  t31os Feb 21 '11 at 8:49
    
@בניית אתרים done! :) –  Wordpressor Feb 21 '11 at 10:54
    
It would be alot easier to debug your problem if the complete code could be shown, there's nothing incorrect as far as i can tell with what you've posted. –  t31os Feb 21 '11 at 13:41

2 Answers 2

up vote 4 down vote accepted

See my comment above...you're using get_field_id() on the name-attribute, where it should be get_field_name().

get_field_id() returns 'widget-'.$this->id_base.'-'.$this->number.'-'.$field_name,
whereas
get_field_name() returns 'widget-'.$this->id_base.'[' . $this->number . '][' . $field_name.']'

share|improve this answer
    
What's the difference if I'm using get_field_id everywhere? Also in the first example that is working? –  Wordpressor Feb 21 '11 at 12:56
    
In the first example you are using get_field_id() on the id attribute and get_field_name() on the name attribute. In the second example you are using get_field_id() on both. Which doesn't work, because now your field value gets registered as $_POST['widget-base-num-name'] instead of $_POST['widget-base']['num']['name']. Obviously, WP maps $_POST['widget-base']['num'] into the first parameter of update(), but it can't do that, so your field value gets lost. –  wyrfel Feb 21 '11 at 16:48

I recently wrote an article about wordpress select boxes on the backend, it might help some of you:

http://wp-dreams.com/wordpress-widget-select-box/

share|improve this answer

Your Answer

 
discard

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.