2 added 121 characters in body
source | link

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:', 'thstlang''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', 'thstlang''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 
}
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:', '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>

    <!-- Widget Article Count -->   
    <p>
        <label for="<?php echo $this->get_field_id('select'); ?>"><?php _e('This is a select menu', 'thstlang'); ?></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 
}

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 
}
1
source | link

Custom widget using get_categories select menu dropdown

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.

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:', '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>

    <!-- Widget Article Count -->   
    <p>
        <label for="<?php echo $this->get_field_id('select'); ?>"><?php _e('This is a select menu', 'thstlang'); ?></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 
}