Want more features on Pastebin? Sign Up, it's FREE!
Guest

Clean WordPress Menu

By: RevelationTravis on May 2nd, 2012  |  syntax: PHP  |  size: 1.16 KB  |  views: 3,325  |  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. //Deletes all CSS classes and id's, except for those listed in the array below
  3. function custom_wp_nav_menu($var) {
  4.         return is_array($var) ? array_intersect($var, array(
  5.                 //List of allowed menu classes
  6.                 'current_page_item',
  7.                 'current_page_parent',
  8.                 'current_page_ancestor',
  9.                 'first',
  10.                 'last',
  11.                 'vertical',
  12.                 'horizontal'
  13.                 )
  14.         ) : '';
  15. }
  16. add_filter('nav_menu_css_class', 'custom_wp_nav_menu');
  17. add_filter('nav_menu_item_id', 'custom_wp_nav_menu');
  18. add_filter('page_css_class', 'custom_wp_nav_menu');
  19.  
  20. //Replaces "current-menu-item" with "active"
  21. function current_to_active($text){
  22.         $replace = array(
  23.                 //List of menu item classes that should be changed to "active"
  24.                 'current_page_item' => 'active',
  25.                 'current_page_parent' => 'active',
  26.                 'current_page_ancestor' => 'active',
  27.         );
  28.         $text = str_replace(array_keys($replace), $replace, $text);
  29.                 return $text;
  30.         }
  31. add_filter ('wp_nav_menu','current_to_active');
  32.  
  33. //Deletes empty classes and removes the sub menu class
  34. function strip_empty_classes($menu) {
  35.     $menu = preg_replace('/ class=""| class="sub-menu"/','',$menu);
  36.     return $menu;
  37. }
  38. add_filter ('wp_nav_menu','strip_empty_classes');
  39. ?>
clone this paste RAW Paste Data