Skip to content

Instantly share code, notes, and snippets.

@egeorjon
Created September 20, 2013 15:08
Show Gist options
  • Save egeorjon/6638967 to your computer and use it in GitHub Desktop.
Save egeorjon/6638967 to your computer and use it in GitHub Desktop.
Remove class and id on the menu generated by wp_nav_menu
<?php
/* -- Remove all class or id -- */
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() : '';
}
/* -- Remove only some class or id -- */
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')) : '';
}
/* -- Remove empty class -- */
add_filter ('wp_nav_menu','strip_empty_classes');
function strip_empty_classes($menu) {
$menu = preg_replace('/ class=(["\'])(?!active).*?\1/','',$menu);
return $menu;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment