Kevin Leary posted a snippet to add aria-expanded tags in 2018. I love the simplicity of it.
He describes what we’re doing here:
This is the recommended approach for “fly-out (or drop-down) menus”
provided by the w3.org, more on fly-out menu accessibility can be read
on their site directly.
function my_nav_menu_link_attributes( $atts, $item, $args ) {
if ( 'main' === $args->theme_location ) {
$item_has_children = in_array( 'menu-item-has-children', $item->classes );
if ( $item_has_children ) {
$atts['aria-haspopup'] = 'true';
$atts['aria-expanded'] = 'false';
}
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'my_nav_menu_link_attributes', 10, 3 );
Related Posts:
- Add custom attribute to menu item link using Filter
- Accesibility problems with dropdown menus in twentyten theme or others
- How to change the wordpress navigation bar UL to OL in Twenty_Twelve similer theme
- SlickNav – Google Lighthouse Accessibility issue for collapsed menu button
- Bootstrap dropdown nav pills not working with wp_nav_menu()
- How to register custom menu widget
- Menu items id’s – are they 100% unique?
- How to var_dump nav menu items from anywhere?
- Links open only on new tab or window
- Show different different menu in theme_location depending on x
- Make Theme automatically choose default nav menu
- Change menu with language?
- Adding Additional Menus In Genesis Child Themes
- Jump links to another page and return back to the first page anchors successfully
- Custom Menus Description Stripping HTML Tags
- How to Create a Custom WordPress Navigation Menu with the Code?
- Show all parent pages and their children below from same author as currently viewed page
- How can I get an array/list of all current WordPress Admin Menu items?
- wp_get_nav_menu_items vs wp_nav_menu
- Does WordPress create mobile menus automatically?
- Remove Page Name From WordPress Menu
- Hiding Menu From Specific Pages
- How can I remove data-target=”#” from bootstrap dropdown menu?
- Use wp_nav_menu to display ALL pages
- Custom dashboard menu does not stay open for the custom Taxonomy within it
- How do I hide one menu item link for logged in and logged out users [closed]
- remove everything from wp_nav_menu
- Menu Item Location
- get_query_var(‘post_type’) not returning in a filter
- How do I apply a class to custom menu items based on user roles
- Menu API not switching menus?
- Navigation Menus Depth for specific menus
- How do you manage large menu hierarchies in WP?
- Using Walker To Add Menu CSS Class Functionality
- How to include the parent page link in this wp_nav_menu walker
- how can remove this element to avoid having two menu buttons with one that shows unnecessary pages
- How to make next and previous button based on a menu
- How to combine multiple categories in one Menu item?
- WordPress Menu Walker: add #hashes to child page links (only)
- add_filter on menu items to add a menu item
- WordPress Walker – Group two items?
- Custom Menu’s – Disable Link checkbox
- Unlimited WordPress Menu
- How to show dynamic side bar on menu item [closed]
- Using my WordPress menu on another platform
- Header and nav menu problem on Thesis site [closed]
- Remove a menu sub-item
- How to remove menu labels in word press?
- modify menu item links dynamically
- Adding numbers inside a span tag in nav menu [closed]
- I’m using wp_nav_menu to display my menu, but I want one more link in there that is a hardcoded link. How can I do this?
- Is there a hook for nav menu item links to add custom css programatically?
- sub menu link is not working
- WordPress Menu adds an unwanted slash before anchor
- Disable parent in wp_list_pages – WordPress
- Add scheduled page or post in the menu section on back-end
- Hoverable menu to appear in different sections of page
- Show Sidebar Menu Subpages When Clicking Parent
- Secondary menu in WP 4.5
- Change link in menu on condition
- Current User In Custom Menu Item URL [closed]
- Creating other page than page.php
- Appearance > Menus > Left Sidebar > Add Toggle
- add_menu_page does not render the expected result
- Custom menu styling
- Getting the WordPress custom menu titles and outputting them in a theme
- Additional navigation bar in header
- Dynamically populate the menu when a new category is created
- Is there any difference between widgets (used in order to manage menus) and register_nav_menus() in terms of performance?
- Menu items not clickable
- How do you show horizontally the submenu in WordPress using the nav walker?
- Adding menu title to custum URL
- Parent Menu Item Requires Two Clicks for Mobile Device
- Add “onclick” option to header menu item
- Full submenu not displaying in wordpress
- Appearance -> Menu editor completely unresponsive
- Custom menu is rendered in all menus
- javascript i wrote not working, can’t figure out why [closed]
- Making my main menu a hamburger menu on all platforms
- menu change after theme change
- Appearance –> Menu -not showing on my main page
- Menu location switcher – example code fails
- how to create pages and link them to a menu
- menu mobile show up even i in desktop mode in Newspaper Theme
- WordPress fountainpress header is covering top bar
- Outputting Page Title As Tooltip
- Current menu item class issue
- How to add class and span to
- Change header menu login-logout without changing main menu
- Appearance > Menus
- extra tag is added in wp_nav_menu
- menu item active class for author.php
- My WordPress menu based on Twitter bootstrap creates menu-item and not page-item
- menu entries are lost
- menu items toggle and display on screen size reduction
- How to get wordpress navigation to work
- How do I show a different secondary navigation menu for each section of my website?
- How to center top Menu bar [closed]
- Changing when mobile menu is displayed
- Navigation menu displays permalinks