Soporte » Plugins y Hacks » Problema con select en widget

  • Tengo este widget donde selecciono una de las 3 notas, sin embargo no me guarda el parametro, no se que estoy haciendo mal.

    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
Viendo 1 respuesta (de un total de 1)
  • Hola jepser, el fallo está en la siguiente función:

    /** @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-><strong>get_field_name</strong>('note'); ?>" class="widefat">
              	<option value="1" <?php if($instance['note'] == '1') echo 'selected="selected"'; ?>>Note 1</option>
                <option value="2" <?php if($instance['note'] == '2') echo 'selected="selected"'; ?>>Note 2</option>
                <option value="3" <?php if($instance['note'] == '3') echo 'selected="selected"'; ?>>Note 3</option>
              </select>
            </p>
            <?php
        }

    En la etiqueta select, hay que poner lo siguiente:

    name=»<?php echo $this->get_field_name(‘note’); ?>»

    (antes llamaba a la función get_field_id). Lo he probado y ya va bien 😉

Viendo 1 respuesta (de un total de 1)
  • El debate ‘Problema con select en widget’ está cerrado a nuevas respuestas.