Example:
<?php class Menu_Fields_Widget extends WP_Widget { function __construct() { // Instantiate the parent object $widget_ops = array( 'classname' => 'widget_title_fields_entries', 'description' => esc_attr__('Menu Field Widget - Created by deshisoft', 'eye-theme') ); $control_ops = array( 'width' => 275 ); parent::__construct( false, 'Menu Field Widget', $widget_ops, $control_ops ); } function widget( $args, $instance ) { extract( $args ); // Widget output $title = apply_filters('widget_title', $instance['title']); // Get menu $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false; if ( !$nav_menu ) return; // Output echo $before_widget; echo '<div style="border:1px solid #ddd; background:#f6f6f6;padding:15px;">'; echo esc_attr($title); wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) ); echo '</div>'; echo $after_widget; } function update( $new_instance, $old_instance ) { // Save widget options $instance = array(); $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['nav_menu'] = (int) $new_instance['nav_menu']; return $instance; } function form( $instance ) { // Output admin widget options form $title = $instance['title']; // Get menus $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : ''; $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) ); // If no menus exists, direct the user to go and create some. if ( !$menus ) { echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>'; return; } ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('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> <!-- Menu --> <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 ( $menus as $menu ) { $selected = $nav_menu == $menu->term_id ? ' selected="selected"' : ''; echo '<option'. $selected .' value="'. $menu->term_id .'">'. $menu->name .'</option>'; } ?> </select></p> <?php } } function menu_field_register_widgets() { register_widget( 'Menu_Fields_Widget' ); } add_action( 'widgets_init', 'menu_field_register_widgets' ); ?>
Theme Folder -> function.php
Add this line:
require get_template_directory() . '/inc/menu_widget.php'; // inc folder name depend on your theme.