How to add your own class to the active link in a WordPress menu
This example shows how you can extend the list of classes for a WordPress menu item and add your own class to the current active menu element.
// Add classes to links
add_filter( 'nav_menu_link_attributes', 'filter_nav_menu_link_attributes', 10, 4 );
function filter_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
if ( $item->current ) {
$class = 'menu-link--active';
$atts['class'] = isset( $atts['class'] ) ? "{$atts['class']} $class" : $class;
}
return $atts;
}—
Note embeded into: nav_menu_link_attributes