I have created a custom widget that is supposed to display a select menu with all the categories the blog has. I have used get_categories to compile the list. This works fine and all categories appear in a drop down menu. Each time I save and refresh the widget page, the custom widget is not there anymore. I checked function update and everything is fine there. So I figured it must be the way I created the form. Any ideas? Thanks in advance.

I didn’t want to dump all the code, so I only pasted the function that creates the form. If you need more just drop a comment

function form( $instance ) {

    /* Default Widget Settings */

    $defaults = array(
        'title' => 'Highlight Category',
        'select'=> 'Option 1'
    );

    $instance = wp_parse_args( (array) $instance, $defaults ); 

?>

    <!-- Widget Title -->
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'lang') ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
    </p>

    <!-- Widget Article Count -->   
    <p>
        <label for="<?php echo $this->get_field_id('select'); ?>"><?php _e('This is a select menu', 'lang'); ?></label>
        <select name="<?php echo $this->get_field_name('select'); ?>" id="<?php echo $this->get_field_id('select'); ?>" class="widefat"> 
            <option value="<?php echo $this->get_field_name('select'); ?>"><?php echo $instance['select']; ?></option> 
            <?php 
             $categories=  get_categories('child_of=0'); 
             foreach ($categories as $category) {
                $option = '<option value="' . $category->cat_name . '" id="' . $this->get_field_id( 'select' ) . '">';
                $option .= $instance['select'];
                $option .= ' ('. $this->get_field_id( 'select' ) .')';
                $option .= '</option>';
                echo $option;
             }
            ?>
        </select>
    </p>

<?php 
}

2 Answers
2

Ok I found a solution thanks to this: Using wp_dropdown_categories in widget options

Here is the code I used instead:

function form( $instance ) {
    /* Default Widget Settings */
    $defaults = array(
        'title' => 'Highlight Category'
    );
    $instance = wp_parse_args( (array) $instance, $defaults ); 
?>

    <!-- Widget Title -->
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'thstlang') ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
    </p>

    <!-- Category Select Menu -->   
    <p>
        <select id="<?php echo $this->get_field_id('kwtax'); ?>" name="<?php echo $this->get_field_name('kwtax'); ?>" class="widefat" style="width:100%;">
            <?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
            <option <?php selected( $instance['kwtax'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
            <?php } ?>      
        </select>
    </p>
<?php 
}

Leave a Reply

Your email address will not be published. Required fields are marked *