Opened 9 years ago
Last modified 3 weeks ago
#37026 accepted defect (bug)
PHP Notice: Trying to get property of non-object in wp-admin\nav-menus.php on line 836
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | 3.0 |
| Component: | Menus | Keywords: | has-patch needs-copy-review changes-requested |
| 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)
Change History (13)
#2
@
6 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 ) : ?>
#4
@
6 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.
#7
@
6 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.
- Add the following code to a plugin:
$locations = get_nav_menu_locations(); $locations['primary'] = 12345; set_theme_mod('nav_menu_locations', $locations); - 🐞 Error condition reproduced
Actual Results
- 🟠 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

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_objectis used.