[wp-trac] [WordPress Trac] #56926: Infinite loop in wp_nav_menu

WordPress Trac noreply at wordpress.org
Fri Nov 18 03:40:32 UTC 2022


#56926: Infinite loop in wp_nav_menu
-------------------------------------------------+-------------------------
 Reporter:  david.binda                          |       Owner:
                                                 |  hellofromTonya
     Type:  defect (bug)                         |      Status:  assigned
 Priority:  normal                               |   Milestone:  6.1.2
Component:  Menus                                |     Version:  6.1
 Severity:  normal                               |  Resolution:
 Keywords:  reporter-feedback has-patch has-     |     Focuses:
  unit-tests needs-refresh                       |
-------------------------------------------------+-------------------------

Comment (by peterwilsoncc):

 As walkers already manage parenting loops, I suspect the best approach may
 be to revert source code changes from the previous commits: [54478,54801].
 The tests can remain.

 Core can then add a default filter to `nav_menu_css_class` to remove the
 child class from the final items. This has the advantage that themes
 wishing to remove the filter will be able to do so and retain the legacy
 behaviour.

 {{{#!php
 <?php
 function fix_56926( $classes, $menu_item, $args, $depth ) {
         // Max-depth is 1-based.
         $max_depth = (int) $args->depth;
         // Depth is 0-based so needs to be increased by one.
         $depth = $depth + 1;

         if ( 0 === $max_depth ) {
                 return $classes;
         }

         if ( -1 === $max_depth || $depth >= $max_depth ) {
                 // Remove the `menu-item-has-children` class.
                 $classes = array_diff( $classes, array( 'menu-item-has-
 children' ) );
         }

         return $classes;
 }
 add_filter( 'nav_menu_css_class', 'fix_56926', 10, 4 );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56926#comment:20>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list