Wordpress add a class to menu link

Adds a class <a class=> https://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_link_attributes

function add_specific_menu_location_atts( $atts, $item, $args ) {
    // check if the item is in the primary menu
    if( $args->menu == 'primary' ) {
      // add the desired attributes:
      $atts['class'] = 'menu-link-class';
    }
    return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );

After creating some WordPress sites based on the Bootstrap framework, i've come up with a solution of easily manipulating the WordPress navigation output by initializing the classes you want. This will make sure your enduser will not be needed to add all the custom classes when the client add's a new menu item.

Maybe it could help someone;

https://github.com/nickkuijpers/WordPress-Extended-Bootstrap-Nav-Walker


You can do it through admin panel also

  • In Appearance > Menus, click the Screen Options tab

  • Under Show advanced menu properties, check CSS Classes

  • Now expand any menu item to reveal the CSS Classes (optional) text
    input.

  • Enter your class name and save your menu to apply the class to the
    menu item

http://sevenspark.com/how-to/how-to-add-a-custom-class-to-a-wordpress-menu-item