Am coding a plugin which creates a wigdet where a user selects from the available categories but I don't how to add selected() and how to check if user selected any option. Below is my code.
<p>
<label for="<?php echo $this->get_field_id('select'); ?>"><?php _e('Select featured Category', 'wp_widget_plugin'); ?></label>
<select name="<?php echo $this->get_field_name('select'); ?>" id="<?php echo $this->get_field_id('select'); ?>" class="widefat">
<option value=""><?php echo esc_attr(__('Select Category')); ?></option>
<?php
$args = array('orderby' => 'name', 'parent' => 0 );
$categories = get_categories($args);
foreach ($categories as $category) {
$option = '<option value="'.$category->cat_ID.'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>
</p>