How To Remove Class From WordPress Menu

Last updated on: August 3rd, 2015

If you have ever built a wordpress theme with Bootstrap front-end framework, you must have worked with wp-bootstrap-navwalker.php file to properly render the menu items. It happens because wordpress by default adds various classes to its navigate menu. Although, this is necessary for extensive customization, but the HTML markup looks cluttered chunk of code that needs to be cleaned up.

To fix this issue you can add only classes that you need and remove pre-loaded classes from the wordpress menu markup. You can add the code below to the functions.php file to tidy it up.

add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
function my_css_attributes_filter($var) {
  return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
}