This can be very useful if you need to add attributes for a menu script.
/** * Add custom attribute and value to a nav menu item's anchor based on CSS class (for MetisMenu) * @author Sridhar Katakam * @link https://sridharkatakam.com/ */ add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) { if ( 'open' === $item->classes[0] ) { $atts['aria-expanded'] = 'false'; } if ( 'open' === $item->classes[0] ) { $atts['class'] = 'has-arrow'; } return $atts; }, 10, 3 );
Reference Links
https://stackoverflow.com/questions/20752318/wordpress-add-a-class-to-menu-link