Simple PHP script - what have I missed?

Discussion in 'PHP' started by Ian Norris, Nov 5, 2013.

  1. #1
    Trying to build a simple WordPress widget plugin, I am stuck on a simple select box:

    <?php
    class wp_my_plugin extends WP_Widget {
    
        // constructor
        function wp_my_plugin() {
            parent::WP_Widget(false, $name = __('Select Test', 'wp_my_plugin') );
        }
    
        // widget form creation
    function form($instance) {
    
    // Check values
    if( $instance) {
        $select = esc_attr($instance['select']);
    } else {
        $select = '';
    }
    ?>
    
    <p>
    <label for="<?php echo $this->get_field_id('select'); ?>"><?php _e('Select', 'wp_widget_plugin'); ?></label>
    <select name="<?php echo $this->get_field_name('select'); ?>" id="<?php echo $this->get_field_id('select'); ?>" class="widefat">
    <?php
    $options = array('lorem', 'ipsum', 'dolorem');
    foreach ($options as $option) {
    echo '<option value="' . $option . '" id="' . $option . '"', $select == $option ? ' selected="selected"' : '', '>', $option, '</option>';
    }
    ?>
    </select>
    </p>
    
    <?php
    }
    
        // update widget
    function update($new_instance, $old_instance) {
          $instance = $old_instance;
          // Fields
          $instance['select'] = strip_tags($new_instance['select']);
        return $instance;
    }
    
        // display widget
    function widget($args, $instance) {
      extract( $args );
      // these are the widget options
      $select = $select['select'];
    
      echo $before_widget;
      // Display the widget
      echo '<div class="widget-text wp_widget_plugin_box">';
    
      // Get $select value
        if ( $select == 'lorem' ) {
            echo 'Lorem option is Selected';
            } else if ( $select == 'ipsum' ) {
            echo 'ipsum option is Selected';
            } else {
            echo 'dolorem option is Selected';
        }
    
      echo '</div>';
      echo $after_widget;
    }
    }   
    // register widget
    add_action('widgets_init', create_function('', 'return register_widget("wp_my_plugin");'));
    
    
    ?>
    Code (markup):
    Can anyone see what I have missed? $select is showing blank...
     
    Ian Norris, Nov 5, 2013 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Well I see a lot of $selects, so it's hard to say what's wrong (and especially where). But I'm not sure this looks right:
    
    extract( $args );
    // these are the widget options
    $select = $select['select'];
    
    PHP:
    Since I don't see $select being declared anywhere before those lines, I'm assuming that this variable comes from $args, which you're extracting?

    So if $args['select'] becomes $select, you're then overriding this variable again with $select['select'], which I assume does not exist? Unless there's an $args['select']['select']?

    It's all just guesses, but it's all I can do with the provided details... But my suggestion is to ditch extract(). It's almost as bad as register_globals.
     
    nico_swd, Nov 6, 2013 IP