2

I'm creating a custom widget for WordPress but am wanting to use the same select menu as the default Custom Menu widget does. So far what I have works except the selected menu isn't being saved or displayed on the frontend. Any help or direction with this is appreciated. Thanks.

For brevity, I've removed extra widget options, so this is basically going to look very close to the Custom Menu widget:

class kedc_mini_sitemap extends WP_Widget {

/*constructor*/
function kedc_mini_sitemap() {
    parent::WP_Widget(false, $name = 'Mini Sitemap');
}

/**/
function widget($args, $instance) {
    extract( $args );

    // widget options
    $title = apply_filters('widget_title', $instance['title']);
    $nav_menu1 = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
    $checkbox = $instance['checkbox'];

    echo $before_widget;    

    if ($title) {
        echo $before_title . $title . $after_title;
    }

    if ($nav_menu1) {
        echo wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu1 ) );
    }


    if ($checkbox == true) {
        echo 'This message is displayed if our checkbox is checked.';
    }

    echo $after_widget;
}

/* saves options chosen from the widgets panel */
function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['nav_menu'] = (int) $new_instance['nav_menu'];
    $instance['checkbox'] = strip_tags($new_instance['checkbox']);
    return $instance;
}

/* display widget in widgets panel */
function form($instance) {  

    $title = esc_attr($instance['title']);
    $nav_menu1 = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
    $checkbox = esc_attr($instance['checkbox']);

    $menus1 = get_terms( 'nav_menu', array( 'hide_empty' => false ) );

    ?>

    <p>
        <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:'); ?></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 $title; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
        <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
            <?php
                foreach ( $menus1 as $menu1 ) {
                    echo '<option value="' . $menu1->term_id . '"'
                        . selected( $nav_menu, $menu1->term_id, false )
                        . '>'. $menu1->name . '</option>';
                }
            ?>
        </select>
    </p>

    <p>
        <input id="<?php echo $this->get_field_id('checkbox'); ?>" name="<?php echo $this->get_field_name('checkbox'); ?>" type="checkbox" value="1" <?php checked( '1', $checkbox ); ?>/>
        <label for="<?php echo $this->get_field_id('checkbox'); ?>"><?php _e('Do you want this to display as 2 columns?'); ?></label>
    </p>

    <?php
}

}

// register widget
add_action('widgets_init', create_function('', 'return register_widget("kedc_mini_sitemap");'));
gstricklind
  • 464
  • 2
  • 6
  • 17
  • And if its possible to be able to select a parent page which could return all 1st level child pages, I'd love to learn how! That would beat creating a new menu group and selecting that. – gstricklind Oct 03 '13 at 23:28

1 Answers1

1

You have a typo in the form() function: $nav_menu1 and $nav_menu.

Use more meaningful, strict and descriptive names for your variables. For example, $menus1 could be called $get_nav_menus.

Use a good IDE, like NetBeans or similar, and check the WordPress Coding Standards Handbook.

Oh, yes, your code is dumping PHP Notices, always develop with WP_DEBUG enabled.

Community
  • 1
  • 1
brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • Ah, thank you! Yes, I had planned on changing the variable names as the nav_menu part was a piece of code I picked up somewhere. I do have debug mode enabled, I'll have to check out why its not giving me errors. Thanks for the heads up and resources. – gstricklind Oct 04 '13 at 15:26