Example:
<?php
class Select_Fields_Widget extends WP_Widget {
function __construct() {
// Instantiate the parent object
$widget_ops = array(
'classname' => 'widget_select_fields_entries',
'description' => esc_attr__('Select Field Widget - Created by deshisoft', 'eye-theme')
);
$control_ops = array(
'width' => 275
);
parent::__construct( false, 'Select Field Widget', $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
// Widget output
$title = apply_filters('widget_title', $instance['title']);
$select = apply_filters('widget_select', $instance['select']);
echo $before_widget;
echo '<div style="border:1px solid #ddd; background:#f6f6f6;padding:15px;">';
echo esc_attr($title);
echo esc_attr($select);
echo '</div>';
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
// Save widget options
$instance = array();
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['select'] = strip_tags( $new_instance['select'] );
return $instance;
}
function form( $instance ) {
// Output admin widget options form
$title = $instance['title'];
$select = $instance['select']; ?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><select id="<?php echo $this->get_field_id('select'); ?>" name="<?php echo $this->get_field_name('select'); ?>" >
<option value="" <?php echo ($instance['select'] === '' ? ' selected="selected"' : ''); ?>>Select Option</option>
<option value="test1" <?php echo ($instance['select'] === 'test1' ? ' selected="selected"' : ''); ?>>Test 1</option>
<option value="test2" <?php echo ($instance['select'] === 'test2' ? ' selected="selected"' : ''); ?> >Test 2</option>
</select></p>
<?php
}
}
function select_field_register_widgets() {
register_widget( 'Select_Fields_Widget' );
}
add_action( 'widgets_init', 'select_field_register_widgets' );
?>
Theme Folder -> function.php
Add this line:
require get_template_directory() . '/inc/select_widget.php'; // inc folder name depend on your theme.