3 * Widget API: WP_Nav_Menu_Widget class
11 * Core class used to implement the Custom Menu widget.
17 class WP_Nav_Menu_Widget extends WP_Widget {
20 * Sets up a new Custom Menu widget instance.
25 public function __construct() {
26 $widget_ops = array( 'description' => __('Add a custom menu to your sidebar.') );
27 parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops );
31 * Outputs the content for the current Custom Menu widget instance.
36 * @param array $args Display arguments including 'before_title', 'after_title',
37 * 'before_widget', and 'after_widget'.
38 * @param array $instance Settings for the current Custom Menu widget instance.
40 public function widget( $args, $instance ) {
42 $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
47 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
48 $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
50 echo $args['before_widget'];
52 if ( !empty($instance['title']) )
53 echo $args['before_title'] . $instance['title'] . $args['after_title'];
55 $nav_menu_args = array(
61 * Filter the arguments for the Custom Menu widget.
64 * @since 4.4.0 Added the `$instance` parameter.
66 * @param array $nav_menu_args {
67 * An array of arguments passed to wp_nav_menu() to retrieve a custom menu.
69 * @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
70 * @type mixed $menu Menu ID, slug, or name.
72 * @param stdClass $nav_menu Nav menu object for the current menu.
73 * @param array $args Display arguments for the current widget.
74 * @param array $instance Array of settings for the current widget.
76 wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
78 echo $args['after_widget'];
82 * Handles updating settings for the current Custom Menu widget instance.
87 * @param array $new_instance New settings for this instance as input by the user via
89 * @param array $old_instance Old settings for this instance.
90 * @return array Updated settings to save.
92 public function update( $new_instance, $old_instance ) {
94 if ( ! empty( $new_instance['title'] ) ) {
95 $instance['title'] = sanitize_text_field( stripslashes( $new_instance['title'] ) );
97 if ( ! empty( $new_instance['nav_menu'] ) ) {
98 $instance['nav_menu'] = (int) $new_instance['nav_menu'];
104 * Outputs the settings form for the Custom Menu widget.
109 * @param array $instance Current settings.
111 public function form( $instance ) {
112 $title = isset( $instance['title'] ) ? $instance['title'] : '';
113 $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
116 $menus = wp_get_nav_menus();
118 // If no menus exists, direct the user to go and create some.
120 <p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
122 if ( isset( $GLOBALS['wp_customize'] ) && $GLOBALS['wp_customize'] instanceof WP_Customize_Manager ) {
123 $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
125 $url = admin_url( 'nav-menus.php' );
128 <?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
130 <div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
132 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
133 <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>"/>
136 <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
137 <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
138 <option value="0"><?php _e( '— Select —' ); ?></option>
139 <?php foreach ( $menus as $menu ) : ?>
140 <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
141 <?php echo esc_html( $menu->name ); ?>