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");'));
Can anyone see what I have missed? $select is showing blankā¦