Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Clean WordPress Menu

By: a guest on May 2nd, 2012  |  syntax: PHP  |  size: 1.19 KB  |  hits: 649  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. function custom_wp_nav_menu($var) {
  3.         return is_array($var) ? array_intersect($var, array(
  4.                 //List of allowed menu classes
  5.                 'current_page_item',
  6.                 'current_page_parent',
  7.                 'current_page_ancestor',
  8.                 'first',
  9.                 'last',
  10.                 'vertical',
  11.                 'horizontal'
  12.                 )
  13.         ) : '';
  14. }
  15. add_filter('nav_menu_css_class', 'custom_wp_nav_menu');
  16. add_filter('nav_menu_item_id', 'custom_wp_nav_menu');
  17. add_filter('page_css_class', 'custom_wp_nav_menu');
  18.  
  19. //Replaces "current-menu-item" with "active"
  20. function current_to_active($text){
  21.         $replace = array(
  22.                 //List of menu item classes that should be changed to "active"
  23.                 'current-menu-item' => 'active',
  24.                 'current_page_parent' => 'active',
  25.                 'current_page_ancestor' => 'active',
  26.         );
  27.         $text = str_replace(array_keys($replace), $replace, $text);
  28.                 return $text;
  29.         }
  30. add_filter('nav_menu_css_class', 'current_to_active');
  31. add_filter('nav_menu_item_id', 'current_to_active');
  32. add_filter('page_css_class', 'current_to_active');
  33.  
  34. //Deletes empty classes and removes the sub menu class
  35. function strip_empty_classes($menu) {
  36.     $menu = preg_replace('/ class=""| class="sub-menu"/','',$menu);
  37.     return $menu;
  38. }
  39. add_filter ('wp_nav_menu','strip_empty_classes');
  40. ?>