How to add custom class in WordPress menu li tag

Simply add the code in functions.php

function dandd_add_additional_class_on_li($classes, $item, $args) {
    if(isset($args->add_li_class)) {
        $classes[] = $args->add_li_class;
    }
    return $classes;
}

add_filter(‘nav_menu_css_class’, ‘dandd_add_additional_class_on_li’, 1, 3);


Use this function in your custom theme file like header.php:

wp_nav_menu(array(
‘theme_location’ => ‘main-menu’,
‘menu_class’     => ‘navbar-nav’,
‘add_li_class’   => ‘nav-item’,
)
);