Make WordPress Core

Opened 13 months ago

Last modified 12 months ago

#58044 new defect (bug)

Issue with wp_setup_nav_menu_item

Reported by: wpweaver's profile wpweaver Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.2
Component: Menus Keywords: needs-patch
Focuses: administration Cc:

Description (last modified by sabernhardt)

/wp-includes/nav-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 but 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
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;
}


Change History (4)

#1 @sabernhardt
13 months ago

  • Description modified (diff)
  • Keywords reporter-feedback added

Thanks for the report!

Is #51565 the same issue?

#2 @wpweaver
13 months ago

Looks as if it could be the same issue - from a couple of years ago. I didn't find it when I searched.

But in this case, that issue may have a bit different cause, or have not been fixed. After tracking down the NULL item at the failure point, I did find such an orphan menu item and removed it which also fixed the issue.

The issue does seem to originate from defining a menu when a menu item no longer exists, or uses a Parent page that no longer exists, and only happened when loading the Customizer. Whatever, call to get_post_states() can pass a null page here. Perhaps the check belongs in get_post_states as it could be called from other instances, but I think it happens only from menu items.

#3 @sabernhardt
13 months ago

  • Keywords reporter-feedback removed

I linked to this on the other ticket. Even if they are not exactly the same issue, they might be fixed by the same code change.

#4 @wpweaver
12 months ago

Follow up:
I just had a report from a user of my theme who encountered the same bug. Saw the error warnings from the same code.

They redefined their menu, and the problem was gone. So it does seem that the problem is with a menu item that has become undefined, probably due to deleting a page.

Just wanted to confirm that this is an unfixed problem, and is likely causing issues for any number of users.

Note: See TracTickets for help on using tickets.