Add active class to the menu li in wordpress

wordpress-menu.png

By default, there is a .current-menu-item class added in the menu li.

we can easily add the active class by simple WordPress filter.

add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if( in_array('current-menu-item', $classes) ){
$classes[] = 'active ';
}
return $classes;
}

That's it..!!!!

Read: WordPress Interview Questions and Answers