Skip to content

Instantly share code, notes, and snippets.

@gstricklind
Last active August 29, 2015 14:02
Show Gist options
  • Save gstricklind/09adca7e2512cd52fe20 to your computer and use it in GitHub Desktop.
Save gstricklind/09adca7e2512cd52fe20 to your computer and use it in GitHub Desktop.
WordPress: Widget - mini sitemap. Allows for menus to be set and with the option to display these in one column or two column. Ideal for theme widget areas that don't need to be too small so the user can use the area for something else later if needed.
<?php
/**
* Create advanced widget for mini sitemap
*/
class kedc_mini_sitemap extends WP_Widget {
/*constructor*/
public function __construct() {
parent::WP_Widget(false, $name = 'Mini Sitemap');
}
/* outputs the content of the widget */
public function widget($args, $instance) {
extract( $args );
// widget options
$title = apply_filters('widget_title', $instance['title']);
$first_menu_title = apply_filters('widget_title', $instance['first_menu_title']);
$get_first_nav_menu = ! empty( $instance['get_first_nav_menu'] ) ? wp_get_nav_menu_object( $instance['get_first_nav_menu'] ) : false;
$second_menu_title = apply_filters('widget_title', $instance['second_menu_title']);
$get_second_nav_menu = ! empty( $instance['get_second_nav_menu'] ) ? wp_get_nav_menu_object( $instance['get_second_nav_menu'] ) : false;
$checkbox = $instance['checkbox'];
echo $before_widget;
if ($title) {
echo $before_title . $title . $after_title;
} else {
echo $before_title . '&nbsp;' . $after_title;
}
if ($checkbox == true) {
$depth = '1';
?>
<div class="mini-sitemap-left">
<?php
if ($first_menu_title) {
echo '<h5>' . $first_menu_title . '</h5>';
}
if ($get_first_nav_menu) {
echo wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $get_first_nav_menu, 'depth' => $depth ) );
}
?>
</div>
<div class="mini-sitemap-right">
<?php
if ($second_menu_title) {
echo '<h5>' . $second_menu_title . '</h5>';
}
if ($get_second_nav_menu) {
echo wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $get_second_nav_menu, 'depth' => $depth ) );
}
?>
</div>
<?php
} else {
// display without divs/columns
if ($first_menu_title) {
echo '<h5>' . $first_menu_title . '</h5>';
}
if ($get_first_nav_menu) {
echo wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $get_first_nav_menu, 'depth' => $depth ) );
}
if ($second_menu_title) {
echo '<h5>' . $second_menu_title . '</h5>';
}
if ($get_second_nav_menu) {
echo wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $get_second_nav_menu, 'depth' => $depth ) );
}
}
echo $after_widget;
}
/* display widget in widgets panel */
public function form($instance) {
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$first_menu_title = isset( $instance['first_menu_title'] ) ? $instance['first_menu_title'] : '';
$get_first_nav_menu = isset( $instance['get_first_nav_menu'] ) ? $instance['get_first_nav_menu'] : '';
$second_menu_title = isset( $instance['second_menu_title'] ) ? $instance['second_menu_title'] : '';
$get_second_nav_menu = isset( $instance['get_second_nav_menu'] ) ? $instance['get_second_nav_menu'] : '';
$checkbox = isset( $instance['checkbox'] ) ? $instance['checkbox'] : '';
$get_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
?>
<!-- Widget Title -->
<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>
<hr />
<!-- First menu title -->
<p>
<label for="<?php echo $this->get_field_id('first_menu_title'); ?>"><?php _e('First Menu Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('first_menu_title'); ?>" name="<?php echo $this->get_field_name('first_menu_title'); ?>" type="text" value="<?php echo $first_menu_title; ?>" />
</p>
<!-- First menu select -->
<p>
<label for="<?php echo $this->get_field_id('get_first_nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
<select id="<?php echo $this->get_field_id('get_first_nav_menu'); ?>" name="<?php echo $this->get_field_name('get_first_nav_menu'); ?>">
<?php
foreach ( $get_menus as $menu ) {
echo '<option value="' . $menu->term_id . '"'
. selected( $get_first_nav_menu, $menu->term_id, false )
. '>'. $menu->name . '</option>';
}
?>
</select>
</p>
<hr />
<!-- Second menu title -->
<p>
<label for="<?php echo $this->get_field_id('second_menu_title'); ?>"><?php _e('Second Menu Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('second_menu_title'); ?>" name="<?php echo $this->get_field_name('second_menu_title'); ?>" type="text" value="<?php echo $second_menu_title; ?>" />
</p>
<!-- Second menu select -->
<p>
<label for="<?php echo $this->get_field_id('get_second_nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
<select id="<?php echo $this->get_field_id('get_second_nav_menu'); ?>" name="<?php echo $this->get_field_name('get_second_nav_menu'); ?>">
<?php
foreach ( $get_menus as $menu ) {
echo '<option value="' . $menu->term_id . '"'
. selected( $get_second_nav_menu, $menu->term_id, false )
. '>'. $menu->name . '</option>';
}
?>
</select>
</p>
<hr />
<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('Display in 2 columns?'); ?></label>
</p>
<?php
}
/* saves options chosen from the widgets panel */
public function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['first_menu_title'] = strip_tags($new_instance['first_menu_title']);
$instance['get_first_nav_menu'] = (int) $new_instance['get_first_nav_menu'];
$instance['second_menu_title'] = strip_tags($new_instance['second_menu_title']);
$instance['get_second_nav_menu'] = (int) $new_instance['get_second_nav_menu'];
$instance['checkbox'] = strip_tags($new_instance['checkbox']);
return $instance;
}
}
// register widget
add_action('widgets_init', create_function('', 'return register_widget("kedc_mini_sitemap");'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment