www.webdeveloper.com
Results 1 to 1 of 1

Thread: Simple PHP script - what have I missed?

  1. #1
    Join Date
    Nov 2013
    Posts
    1

    Simple PHP script - what have I missed?

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

    PHP Code:
    <?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");'));
     
     
    ?>
    Can anyone see what I have missed? $select is showing blank...
    Last edited by NogDog; 11-05-2013 at 02:40 PM. Reason: changed code tags to php tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles