• Ladies and gents,

    I’m working on a widget and in that widget I want a few <select> fields and a few URL fields. In the example I’m going to give, you see the URL fields. I want to put those fields in an array and place that in the DB and read that same array out again.

    I’m not sure what I’m doing wrong, but all the fields get the value ‘array’ now.

    <?php
    function form($instance) {
            $url = esc_attr($instance['url']);
            for($i=0;$i<5;$i++) {
            ?>
            <p>
                <?php print_r($url); ?>
    			<label for="<?php echo $this->get_field_id('url'); ?>"><?php _e('URL:'); ?>
    				<input class="widefat" id="<?php echo $this->get_field_id('url'); ?>-<?php echo $i; ?>" name="<?php echo $this->get_field_name('url'); ?>[]" type="text" value="<?php echo $url; ?>" />
    			</label>
    		</p>
    	<?php
            }
            ?>
    <?php
    function update($new_instance,$old_instance) {
    		$instance = $old_instance;
    
            for($i=0;$i<5;$i++) {
                $instance['url'] = esc_attr($new_instance['url']);
            }
    
    		return $instance;
    	}
    ?>

    I hope some one can give me a shove in the right direction.

    Thanks

The topic ‘How to save array values in a widget’ is closed to new replies.