Skip to content

Instantly share code, notes, and snippets.

@kaseybon
Created December 13, 2013 19:29
Show Gist options
  • Save kaseybon/7949819 to your computer and use it in GitHub Desktop.
Save kaseybon/7949819 to your computer and use it in GitHub Desktop.
WordPress function to remove unnecessary classes from menu items.
<?php
function cleanname($v) {
$v = preg_replace('/[^a-zA-Z0-9s]/', '', $v);
$v = str_replace(' ', '-', $v);
$v = strtolower($v);
return $v;
}
// Reduce nav classes, leaving only 'current-menu-item'
function nav_class_filter( $var ) {
return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
}
add_filter('nav_menu_css_class', 'nav_class_filter', 100, 1);
// Add page slug as nav IDs
function nav_id_filter( $id, $item ) {
return 'nav-'.cleanname($item->title);
}
add_filter( 'nav_menu_item_id', 'nav_id_filter', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment