Make WordPress Core

Changeset 59921


Ignore:
Timestamp:
03/03/2025 10:51:24 PM (7 weeks ago)
Author:
joedolson
Message:

Bundled Themes: Twenty Nineteen: Omit irrelevant ARIA in menus.

Don't set aria-expanded and aria-haspopup attributes outside of the primary menu. Secondary menus (social and footer) are set to a depth of 1, and have nothing to expand or popup.

Props bschneidewind, joedolson, mikinc860.
Fixes #62896.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentynineteen/inc/template-functions.php

    r59218 r59921  
    168168 * @link https://www.w3.org/WAI/tutorials/menus/flyout/
    169169 *
    170  * @param array   $atts {
     170 * @param array    $atts {
    171171 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
    172172 *
     
    177177 *     @type string $aria-current The aria-current attribute.
    178178 * }
    179  * @param WP_Post $item The current menu item object.
     179 * @param WP_Post  $item The current menu item object.
     180 * @param stdClass $args An object of `wp_nav_menu()` arguments.
    180181 * @return string[] Modified attributes.
    181182 */
    182 function twentynineteen_nav_menu_link_attributes( $atts, $item ) {
    183 
    184     // Add [aria-haspopup] and [aria-expanded] to menu items that have children.
    185     $item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
    186     if ( $item_has_children ) {
    187         $atts['aria-haspopup'] = 'true';
    188         $atts['aria-expanded'] = 'false';
     183function twentynineteen_nav_menu_link_attributes( $atts, $item, $args ) {
     184
     185    // Check that this is the primary menu.
     186    if ( isset( $args->theme_location ) && 'menu-1' === $args->theme_location ) {
     187        // Add [aria-haspopup] and [aria-expanded] to menu items that have children.
     188        $item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
     189        if ( $item_has_children ) {
     190            $atts['aria-haspopup'] = 'true';
     191            $atts['aria-expanded'] = 'false';
     192        }
    189193    }
    190194
    191195    return $atts;
    192196}
    193 add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 2 );
     197add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 3 );
    194198
    195199/**
Note: See TracChangeset for help on using the changeset viewer.