Skip to content

Instantly share code, notes, and snippets.

@jmsmrgn
Last active February 8, 2019 11:40
Show Gist options
  • Select an option

  • Save jmsmrgn/4034557 to your computer and use it in GitHub Desktop.

Select an option

Save jmsmrgn/4034557 to your computer and use it in GitHub Desktop.

Revisions

  1. jmsmrgn renamed this gist Aug 19, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @morgchopz morgchopz renamed this gist Feb 14, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @morgchopz morgchopz created this gist Nov 7, 2012.
    38 changes: 38 additions & 0 deletions clean_wp_nav_menu
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    //Deletes all CSS classes and id's, except for those listed in the array below
    function custom_wp_nav_menu($var) {
    return is_array($var) ? array_intersect($var, array(
    //List of allowed menu classes
    'first',
    'last',
    'current_page_item',
    'current_page_parent',
    'current_page_ancestor',
    'current-menu-ancestor',
    'active'
    )
    ) : '';
    }
    add_filter('nav_menu_css_class', 'custom_wp_nav_menu');
    add_filter('nav_menu_item_id', 'custom_wp_nav_menu');
    add_filter('page_css_class', 'custom_wp_nav_menu');

    //Replaces "current-menu-item" (and similar classes) with "active"
    function current_to_active($text){
    $replace = array(
    //List of menu item classes that should be changed to "active"
    'current_page_item' => 'active',
    'current_page_parent' => 'active',
    'current_page_ancestor' => 'active',
    'current-menu-ancestor' => 'active'
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
    }
    add_filter ('wp_nav_menu','current_to_active');

    //Deletes empty classes and removes the sub menu class
    function strip_empty_classes($menu) {
    $menu = preg_replace('/ class=""| class="sub-menu"/','',$menu);
    return $menu;
    }
    add_filter ('wp_nav_menu','strip_empty_classes');