Make WordPress Core

Opened 10 years ago

Closed 3 weeks ago

Last modified 3 weeks ago

#37026 closed defect (bug) (fixed)

PHP Notice: Trying to get property of non-object in wp-admin\nav-menus.php on line 836

Reported by: skylarkcob's profile skylarkcob Owned by: sabernhardt's profile sabernhardt
Milestone: 7.0 Priority: normal
Severity: normal Version: 3.0
Component: Menus Keywords: has-patch needs-copy-review
Focuses: Cc:

Description

I'm using WordPress 4.5.2 and get this PHP Notice:

PHP Notice:  Trying to get property of non-object in wp-admin\nav-menus.php on line 836

When I export and import database for using on localhost (one database content for two sites), two sites using different theme and have different menu location.

My first site has menu location Mobile, when I use this database on new site with Twenty Sixteen theme, it doesn't have this location, so the notice message appeared.

The menu location on Menu Settings section looks like this:

Mobile (Currently set to: )

Plese add a conditional function to check if is a nav menu object before get its name.

wp_get_nav_menu_object( $menu_locations[ $location ] )->name

It's not an error, but I don't want to see this notice message when coding theme. Thank you.

Attachments (3)

37026.patch (1.5 KB) - added by Frozzare 10 years ago.
37026.2.patch (1.0 KB) - added by sabernhardt 9 months ago.
uses is_nav_menu() to check for false, an error or if the taxonomy is not 'nav_menu'
37026.3.patch (1.0 KB) - added by apermo 9 months ago.
Improved order in if from cheapest to most expensive for good measure.

Download all attachments as: .zip

Change History (24)

@Frozzare
10 years ago

#1 @Frozzare
10 years ago

  • Keywords has-patch added

Added a condition checking if the menu can be used before it's used. This condition exists in other places in the core when wp_get_nav_menu_object is used.

@sabernhardt
9 months ago

uses is_nav_menu() to check for false, an error or if the taxonomy is not 'nav_menu'

#2 @sabernhardt
9 months ago

  • Milestone set to Future Release

Related: #63484

At first, checking for false seemed to be enough because the docblock for wp_get_nav_menu_object() said it returns either WP_Term or false.
<?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] !== $nav_menu_selected_id && wp_get_nav_menu_object( $menu_locations[ $location ] ) ) : ?>

However, is_nav_menu() also checks for an error or incorrect taxonomy, so I used that instead in the refreshed patch.
<?php if ( ! empty( $menu_locations[ $location ] ) && is_nav_menu( $menu_locations[ $location ] ) && $menu_locations[ $location ] !== $nav_menu_selected_id ) : ?>

#3 @apermo
9 months ago

#63484 was marked as a duplicate.

#4 @apermo
9 months ago

I've reviewed @sabernhardt s Patch and it will fix #63484 and this since the additional call of is_nav_menu( $menu_locations[ $location ] ) will prevent wp_get_nav_menu_object() from returning false.

One minor improvement on it, I've changed the order, so that is_nav_menu( $menu_locations[ $location ] ) as the most expensive function will be called last, it likely won't make any measurable difference since it's backend code that's not regularly called, but it's good practice to sort conditions from cheap to expensive. And there are performance tickets just doing this.

@apermo
9 months ago

Improved order in if from cheapest to most expensive for good measure.

#5 @apermo
9 months ago

  • Version changed from 4.5.2 to 3.0

#6 @sabernhardt
9 months ago

  • Milestone changed from Future Release to 6.9

#7 @SirLouen
9 months ago

  • Keywords needs-copy-review changes-requested added

Combined Bug Reproduction and Patch Testing Report

Description

🟠 This report validates that the indicated patch works but with some caveats

Patch tested: https://core.trac.wordpress.org/attachment/ticket/37026/37026.3.patch

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.27.5
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-One 2.5
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

Testing Instructions

I cannot really reproduce this through regular, only reproducible under certain forced conditions where regular menus are not being used (as the scenario commented by the OP). The only solution comes to adding a code to a plugin, to forcefully set an unexistent menu to an existing location.

  1. Add the following code to a plugin:
    $locations = get_nav_menu_locations();
    $locations['primary'] = 12345; 
    set_theme_mod('nav_menu_locations', $locations);
    
  2. 🐞 Error condition reproduced

Actual Results

  1. 🟠 Issue resolved with patch with concerns

Additional Notes

  • I have one main concern. With the patch, the fact is that the location is still assigned to an inexistent menu and nothing is informing of such. I feel that this patch is just hiding the error, but not dealing with it. In fact, the error is posing something that is intrinsically wrong. You can deal with the error by simply going into Menu > Manage Locations > And assigning the right Menu to the conflicting location.
  • My suggestion would be to add a notice explaining what is going on with this error. Something like

"Your $menu_locations[ $location ] location has a wrong menu assigned. Please, assign a valid menu"

  • I'm assuming that my error is a little different because it has been 9 years after the report was done.

Supplemental Artifacts

Error condition

https://i.imgur.com/WGc42Q3.png

Last edited 4 months ago by SirLouen (previous) (diff)

This ticket was mentioned in Slack in #core by welcher. View the logs.


4 months ago

#9 @welcher
4 months ago

  • Milestone changed from 6.9 to 7.0

This was reviewed in the 6.9 Bug Scrub today. The patch need a refresh and we're 1 day away from Beta 2 so I will move it the 7.0 milestone.

#10 @sabernhardt
4 months ago

  • Owner set to sabernhardt
  • Status changed from new to accepted

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


5 weeks ago

#12 @audrasjb
5 weeks ago

As per today's bug scrub, I'm leaving here some notes from the scrub:
https://wordpress.slack.com/archives/C02RQBWTW/p1770137531074749
"Instead of an error message, I might make the text something like "(Currently set to: an invalid menu ID)". The theme-location-set span should also have an id attribute for aria-describedby."

This ticket was mentioned in PR #10859 on WordPress/wordpress-develop by @sabernhardt.


5 weeks ago
#13

  • Creates a text string to display for invalid menu ID.
  • Assigns id and aria-describedby to the connect the span with the checkboxes.

Trac 37026

@sabernhardt commented on PR #10859:


5 weeks ago
#14

Before the patch, an invalid menu ID can result in a PHP error:
https://github.com/user-attachments/assets/359e9905-2631-40c1-b36a-d239cb0dec78

With the patch, the text says that the ID is invalid (but it is not an error message):
https://github.com/user-attachments/assets/eddad662-8be0-4674-b952-816b73a5b865

Note: To assign an invalid ID, I needed to edit the theme_mods_twentytwentyone value in the database, in the *_options table.

@mukesh27 commented on PR #10859:


5 weeks ago
#15

@sabernhardt Request some changes on PR.

#16 @sabernhardt
4 weeks ago

  • Keywords changes-requested removed

As of today, the pull request shows this when the ID is not a valid menu:

(Currently set to: an unknown menu)

@westonruter commented on PR #10859:


3 weeks ago
#17

@audrasjb Are you intending to commit?

@audrasjb commented on PR #10859:


3 weeks ago
#18

@westonruter yep

#19 @audrasjb
3 weeks ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 61658:

Menus: Prevent error in Menu location checkbox settings.

This changeset avoids throwing a PHP error when an invalid ID is assigned to a menu location, creates a text string to display when using an invalid menu ID, and assigns id and aria-describedby attributes to connect the span element with the related checkbox.

Props skylarkcob, Frozzare, sabernhardt, apermo, SirLouen, audrasjb, mukesh27, westonruter.
Fixes #37026.

@audrasjb commented on PR #10859:


3 weeks ago
#20

fixed

@sabernhardt commented on PR #10859:


3 weeks ago
#21

committed in r61658

Note: See TracTickets for help on using tickets.