WordPress.org

Ready to get started?Download WordPress

Forums

Widget and save problem (1 post)

  1. jepser
    Member
    Posted 3 years ago #

    Hi! I have the following code of a widget where I want to save the value: 1, 2, 3, and depending of the case load an image. The problem is that the value isn't saved.

    class noteWidget extends WP_Widget {
        /** constructor */
        function noteWidget() {
            parent::WP_Widget(false, $name = 'Note Widget');
        }
    
        /** @see WP_Widget::widget */
        function widget($args, $instance) {
            extract( $args );
            $note = $instance['note'];
            ?>
            	<pre><?php print_r($instance); ?></pre>
            		<?php echo $before_widget;
    					switch ($note){
    						case '1':
    							echo '<img src="' . get_bloginfo('stylesheet_directory') .'/images/note1.png" alt="Note 1" />';
    						break;
    						case '2':
    							echo '<img src="' . get_bloginfo('stylesheet_directory') .'/images/note2.png" alt="Note 2" />';
    						break;
    						case '3';
    							echo '<img src="' . get_bloginfo('stylesheet_directory') .'/images/note3.png" alt="Note 3" />';
    						break;
    					}
    				?>
                    <?php echo $after_widget; ?>
            <?php
        }
    
        /** @see WP_Widget::update */
        function update($new_instance, $old_instance) {
    	$instance = $old_instance;
    	$instance['note'] = strip_tags($new_instance['note']);
            return $instance;
        }
    
        /** @see WP_Widget::form */
        function form($instance) {
            $note = esc_attr($instance['note']);
            ?>
            <pre><?php print_r($instance); ?></pre>
             <p>
              <label for="<?php echo $this->get_field_id('note'); ?>"><?php _e('Choose a note:'); ?></label>
              <select id="<?php echo $this->get_field_id('note'); ?>" name="<?php echo $this->get_field_id('note'); ?>" class="widefat">
              	<option value="1" <?php if($instance['format'] == '1') echo 'selected="selected"'; ?>>Note 1</option>
                <option value="2" <?php if($instance['format'] == '2') echo 'selected="selected"'; ?>>Note 2</option>
                <option value="3" <?php if($instance['format'] == '3') echo 'selected="selected"'; ?>>Note 3</option>
              </select>
            </p>
            <?php
        }
    
    } // class noteWidget

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags