[wp-trac] [WordPress Trac] #58044: Issue with wp_setup_nav_menu_item

WordPress Trac noreply at wordpress.org
Sat Apr 1 16:20:53 UTC 2023


#58044: Issue with wp_setup_nav_menu_item
----------------------------+-----------------------------
 Reporter:  wpweaver        |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Menus           |    Version:  6.2
 Severity:  normal          |   Keywords:  needs-patch
  Focuses:  administration  |
----------------------------+-----------------------------
 /wp-includes/nave-menu.php has an issue handling bad $menu_item.


 The problem is with trying to call **get_post_status(**). If a post with
 type ''nav_menu_item'' has a problem (I think not having a correct parent
 or something), the call to **get_post($menu_item->object_id)** returns
 NULL which then causes an error message from **get_post_states()**.

 I had to search the site database to find the offending menu item on my
 theme development site and deleted the bad menu.  I think I likely deleted
 a page or post that had been include in a menu definition, or perhaps it
 was because I typically use the Parent Page to define the default menu,
 which is still supported bu seldom used.

 This error only seems to appear when using the Customizer, I'm guessing
 while it is generating the ''Menus'' option. Didn't get error from
 Dashboard Menu, nor have I tried this on a block theme.

 The problem is found around line 833 in nav-menu.php.

 Here's a possible fix which worked with my bad menu definition, but the
 problem might be deeper.

 {{{#!php
 <?php
 if ( $object ) {
     $menu_item->type_label = $object->labels->singular_name;
     // Denote post states for special pages (only in the admin).
     if ( function_exists( 'get_post_states' ) ) {
         $menu_post   = get_post( $menu_item->object_id );
         /*
          * Suggested fix for bad nav_menu_item post - likely caused by now
 missing parent
          *  which can result in $menu_post being NULL at this point
          *
          */
         if ($menu_post != NULL) {   //*** add a NULL check
             $post_states = get_post_states($menu_post);
             if ($post_states) {
                 $menu_item->type_label = wp_strip_all_tags(implode(', ',
 $post_states));
             }
         } // end of added NULL check
     }
 } else {
     $menu_item->type_label = $menu_item->object;
     $menu_item->_invalid   = true;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58044>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list