I use this widget in order to show subcategories on my site and it
works. I tried to edit it in order to add dropdown box feature (like
in the default category widget ) and it wont work properly. Can you tell me where the error is ? Thx


<?php
/*
Plugin Name: SubCategories Widget
Description: This Widget lists the sub-categories for a given category and displays it in dropdown box.
Author: John Doe
Version: 1.0

*/

class SubCategoriesWidget extends WP_Widget {

function SubCategoriesWidget() {
$widget_ops = array('classname' => 'widget_sub_categories', 'description' => __('Lists the sub-categories for a given category.', 'sub_categories') );
$this->WP_Widget('sub_categories_widget', __('Sub Categories', 'sub_categories'), $widget_ops);
}

function widget($args, $instance) {
extract($args, EXTR_SKIP);

$category_id = empty($instance['category_id']) ? 1 : $instance['category_id'];
$show_post_count = empty($instance['show_post_count']) ? 0 : $instance['show_post_count'];
$sub_subs = empty($instance['sub_subs']) ? 0 : $instance['sub_subs'];
$d = ! empty( $instance['dropdown'] ) ? '1' : '0';

if ($use_cat_title) {
$title = apply_filters('widget_title', get_cat_name($category_id), $instance, $this->id_base);
} else {
$title = apply_filters('widget_title', empty($instance['title'] ) ? __('Sub Categories', 'sub_categories') : $instance['title'], $instance, $this->id_base);
}

$parent = $sub_subs == 1 ? 'child_of' : 'parent';

$subs = get_categories(array($parent => $category_id, 'hide_empty' => $hide_empty_cats, 'show_count' => $show_post_count, 'exclude' => $excluded));

if (!empty($subs)) {

echo $before_widget;

if ($title_link) {
echo $before_title.'<a href="'.get_category_link($category_id).'">'.$title.'</a>'.$after_title;
} else {
echo $before_title.$title.$after_title;
}

echo '<ul>';
wp_list_categories(array($parent => $category_id, 'hide_empty' => $hide_empty_cats, 'show_count' => $show_post_count, 'exclude' => $excluded, 'title_li' => null));
echo '</ul>';
echo $after_widget;
}
if ( $d ) {
$cat_args['show_option_none'] = __('Select Category');
wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));

?>

<script type='text/javascript'>

var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;

</script>

}
}

<?php
function update($new_instance, $old_instance) {

$instance = $old_instance;

$instance['title'] = trim(strip_tags($new_instance['title']));
$instance['category_id'] = (int) $new_instance['category_id'];
$instance['show_post_count'] = (int) $new_instance['show_post_count'];
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;


return $instance;
}


function form($instance) {

$instance = wp_parse_args((array) $instance, array('title' => __('Sub Categories', 'sub_categories'), 'category_id' => 1, 'use_cat_title' => 0, 'hide_empty_cats' => 0, 'show_post_count' => 1, 'title_link' => 0, 'excluded' => '', 'sub_subs' => 0 ));

$excluded = esc_attr($instance['excluded']);
?>


<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:', 'sub_categories'); ?></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 esc_attr($instance['title']) ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('category_id'); ?>"><?php _e('Parent Category:', 'sub_categories'); ?></label>
<select id="<?php echo $this->get_field_id('category_id'); ?>" name="<?php echo $this->get_field_name('category_id'); ?>">

<?php
$categories = get_categories(array('hide_empty' => 0));
foreach ($categories as $cat) {
if ($cat->cat_ID == $instance['category_id']) {
$option = '<option selected="selected" value="'.$cat->cat_ID.'">';
} else {
$option = '<option value="'.$cat->cat_ID.'">';
}
$option .= $cat->cat_name.'</option>';
echo $option;
}
?>
</select>
</p>

<p>
<input id="<?php echo $this->get_field_id('show_post_count'); ?>" name="<?php echo $this->get_field_name('show_post_count'); ?>" type="checkbox" value="1" <?php if ($instance['show_post_count']) echo 'checked="checked"'; ?>/>
<label for="<?php echo $this->get_field_id('show_post_count'); ?>"><?php _e('Show Post Count', 'sub_categories'); ?></label>
</p>




<?php
}
}

add_action('widgets_init', create_function('', 'return register_widget("SubCategoriesWidget");'));

?>