Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #63261


Ignore:
Timestamp:
04/10/2025 09:16:26 PM (7 months ago)
Author:
sabernhardt
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #63261

    • Property Focuses coding-standards removed
  • Ticket #63261 – Description

    initial v1  
    11== Description ==
    2 If a page and a taxonomy term (e.g., product_cat) share the same numeric ID, both can be incorrectly marked as active in menus (current-menu-item, aria-current="page").
     2If a page and a taxonomy term (e.g., `product_cat`) share the same numeric ID, both can be incorrectly marked as active in menus (`current-menu-item`, `aria-current="page"`).
    33
    4 This occurs because WordPress checks only the object_id when determining the active menu item, without verifying that the object types match.
     4This occurs because WordPress checks only the `object_id` when determining the active menu item, without verifying that the object types match.
    55
    66== Steps to Reproduce ==
     
    88Create a WordPress page (e.g., "About") — assume it has ID 23.
    99
    10 Create a WooCommerce product category that also ends up with term_id = 23.
     10Create a WooCommerce product category that also ends up with `term_id` = 23.
    1111
    1212Add both items to a navigation menu.
    1313
    14 Visit the product category archive (/product-category/boots/).
     14Visit the product category archive (`/product-category/boots/`).
    1515
    1616Observe that ''both'' menu items ("About" and "Boots") are marked as current.
     
    2323
    2424== Cause ==
    25 In wp-includes/nav-menu-template.php, this comparison is made:
     25In `wp-includes/nav-menu-template.php`, this comparison is made:
    2626
    27 {{{php if ( $menu_item->object_id === $queried_object_id ) { $menu_item->current = true; } }}}
     27{{{#!php
     28if ( $menu_item->object_id === $queried_object_id ) { $menu_item->current = true; }
     29}}}
    2830
    29 This does not check if the object type (e.g. page, product_cat) also matches.
     31This does not check if the object type (e.g. `page`, `product_cat`) also matches.
    3032
    3133== Proposed Fix ==
    3234Enhance the condition to ensure both ID and object type match:
    3335
    34 {{{php $queried_object_type = ''; if ( isset( $queried_object->post_type ) ) { $queried_object_type = $queried_object->post_type; } elseif ( isset( $queried_object->taxonomy ) ) { $queried_object_type = $queried_object->taxonomy; } if ( (int) $menu_item->object_id === (int) $queried_object_id && $menu_item->object === $queried_object_type ) { $menu_item->current = true; } }}}
     36{{{#!php
     37$queried_object_type = '';
     38if ( isset( $queried_object->post_type ) ) {
     39    $queried_object_type = $queried_object->post_type;
     40} elseif ( isset( $queried_object->taxonomy ) ) {
     41    $queried_object_type = $queried_object->taxonomy;
     42}
     43if ( (int) $menu_item->object_id === (int) $queried_object_id
     44    && $menu_item->object === $queried_object_type ) {
     45    $menu_item->current = true;
     46}
     47}}}
    3548
    3649== Environment ==