[wp-trac] [WordPress Trac] #58932: wp_setup_nav_menu_item() throws PHP warning when using virtual menu-items

WordPress Trac noreply at wordpress.org
Sat Jul 29 00:25:24 UTC 2023


#58932: wp_setup_nav_menu_item() throws PHP warning when using virtual menu-items
--------------------------+-----------------------------
 Reporter:  apedog        |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  5.4
 Severity:  normal        |   Keywords:  has-patch
  Focuses:                |
--------------------------+-----------------------------
 I believe this bug was introduced in [changeset:"47211"]

 === TLDR ===
 `wp_setup_nav_menu_item()` calls `get_post()` but does no sanity checking
 before calling `get_post_states()` with what might be a null. Adding a
 simple `if ( $menu_post !== null )` before calling `get_post_states()`
 resolves this issue.

 I've added a patch.

 === Details ===
 I have an old plugin (pre-dating above commit) that renders custom
 dynamically-generated drop-down menus (eg. recent posts, yearly archives).
 The plugin is somewhat based on https://www.ibenic.com/understanding-
 wordpress-menu-items/ but is a bit more involved. It's a stable and simple
 plugin.

 The way the plugin works is it adds a single menu-item meta-box in the
 Menu editor page. However on the front it dynamically generates submenu
 items that don't show on the Edit Menu page. On the front the plugin
 creates virtual/fake `WP_Post` objects at runtime that are fed into
 `Walker_Nav_Menu_Checklist`. Therefore these objects have no corresponding
 `$menu_post` in the database.

 Since WordPress 5.4, core throws multiple `Attempt to read property
 "post_status|ID" on null` notices on every page load. These notices have
 been upgraded to warnings in PHP 8.

 {{{
 wp-admin/includes/template.php:2288
 get_post_states()
 wp-includes/nav-menu.php:839
 wp_setup_nav_menu_item()
 wp-includes/nav-menu.php:839
 array_map()
 wp-includes/nav-menu.php:727
 wp_get_nav_menu_items()
 wp-admin/nav-menus.php:596
 }}}

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


More information about the wp-trac mailing list