Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 
}
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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 
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.