WP_Nav_Menu_Widget

Core class used to implement the Custom Menu widget.

Defined (1)

The class is defined in the following location(s).

/wp-includes/widgets/class-wp-nav-menu-widget.php  
  1. class WP_Nav_Menu_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up a new Custom Menu widget instance. 
  5. * @since 3.0.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $widget_ops = array( 
  10. 'description' => __( 'Add a custom menu to your sidebar.' ),  
  11. 'customize_selective_refresh' => true,  
  12. ); 
  13. parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops ); 
  14.  
  15. /** 
  16. * Outputs the content for the current Custom Menu widget instance. 
  17. * @since 3.0.0 
  18. * @access public 
  19. * @param array $args Display arguments including 'before_title', 'after_title',  
  20. * 'before_widget', and 'after_widget'. 
  21. * @param array $instance Settings for the current Custom Menu widget instance. 
  22. */ 
  23. public function widget( $args, $instance ) { 
  24. // Get menu 
  25. $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false; 
  26.  
  27. if ( !$nav_menu ) 
  28. return; 
  29.  
  30. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  31. $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); 
  32.  
  33. echo $args['before_widget']; 
  34.  
  35. if ( !empty($instance['title']) ) 
  36. echo $args['before_title'] . $instance['title'] . $args['after_title']; 
  37.  
  38. $nav_menu_args = array( 
  39. 'fallback_cb' => '',  
  40. 'menu' => $nav_menu 
  41. ); 
  42.  
  43. /** 
  44. * Filters the arguments for the Custom Menu widget. 
  45. * @since 4.2.0 
  46. * @since 4.4.0 Added the `$instance` parameter. 
  47. * @param array $nav_menu_args { 
  48. * An array of arguments passed to wp_nav_menu() to retrieve a custom menu. 
  49. * @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty. 
  50. * @type mixed $menu Menu ID, slug, or name. 
  51. * } 
  52. * @param stdClass $nav_menu Nav menu object for the current menu. 
  53. * @param array $args Display arguments for the current widget. 
  54. * @param array $instance Array of settings for the current widget. 
  55. */ 
  56. wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) ); 
  57.  
  58. echo $args['after_widget']; 
  59.  
  60. /** 
  61. * Handles updating settings for the current Custom Menu widget instance. 
  62. * @since 3.0.0 
  63. * @access public 
  64. * @param array $new_instance New settings for this instance as input by the user via 
  65. * WP_Widget::form(). 
  66. * @param array $old_instance Old settings for this instance. 
  67. * @return array Updated settings to save. 
  68. */ 
  69. public function update( $new_instance, $old_instance ) { 
  70. $instance = array(); 
  71. if ( ! empty( $new_instance['title'] ) ) { 
  72. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  73. if ( ! empty( $new_instance['nav_menu'] ) ) { 
  74. $instance['nav_menu'] = (int) $new_instance['nav_menu']; 
  75. return $instance; 
  76.  
  77. /** 
  78. * Outputs the settings form for the Custom Menu widget. 
  79. * @since 3.0.0 
  80. * @access public 
  81. * @param array $instance Current settings. 
  82. * @global WP_Customize_Manager $wp_customize 
  83. */ 
  84. public function form( $instance ) { 
  85. global $wp_customize; 
  86. $title = isset( $instance['title'] ) ? $instance['title'] : ''; 
  87. $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : ''; 
  88.  
  89. // Get menus 
  90. $menus = wp_get_nav_menus(); 
  91.  
  92. // If no menus exists, direct the user to go and create some. 
  93. ?> 
  94. <p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>> 
  95. <?php 
  96. if ( $wp_customize instanceof WP_Customize_Manager ) { 
  97. $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();'; 
  98. } else { 
  99. $url = admin_url( 'nav-menus.php' ); 
  100. ?> 
  101. <?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?> 
  102. </p> 
  103. <div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>> 
  104. <p> 
  105. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label> 
  106. <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 ); ?>"/> 
  107. </p> 
  108. <p> 
  109. <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label> 
  110. <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>"> 
  111. <option value="0"><?php _e( '— Select —' ); ?></option> 
  112. <?php foreach ( $menus as $menu ) : ?> 
  113. <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>> 
  114. <?php echo esc_html( $menu->name ); ?> 
  115. </option> 
  116. <?php endforeach; ?> 
  117. </select> 
  118. </p> 
  119. <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?> 
  120. <p class="edit-selected-nav-menu" style="<?php if ( ! $nav_menu ) { echo 'display: none;'; } ?>"> 
  121. <button type="button" class="button"><?php _e( 'Edit Menu' ) ?></button> 
  122. </p> 
  123. <?php endif; ?> 
  124. </div> 
  125. <?php