wordpress snippets last updated   : August 18, 2014
we now have 638 snippets

Remove every class and id from the wp_nav_menu

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will remove nearly every class and id from the wp_nav_menu except for the current-menu-item class. This is a great little snippet that will make your wp_nav_menu a little cleaner enjoy.

snippet :  PHPcopy
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')) : '';
}
source →

Did you like this post? If so please share it with your friends. Thanks!

  • http://electronicsnewsline.com/ Electronics News

    Thanks for sharing this coding, will try at once, nice idea…..

    • http://wpsnipp.com Kevin Chard

      Cool glad to help.

      • Jeremy Smith

        How would you go about making one of the li elements with an ID? Like I mean you’d have say a ul list of 6 links for example and one of them (the first one for arguments sake) would be oh I don’t know with the id ‘login’?

        Like so:

        Login
        Carry on with the rest

        Be interesting to know

  • William

    should i put it under any functions? or i just paste the code on my functions.php?~~

  • Pesh

    this renders the custom classes that you can add via the menus backend useless (they are stored as post meta associated with the menu item), so you have to also merge those into the returned array if you want to use them

  • Pesh

    this renders the custom classes that you can add via the menus backend useless (they are stored as post meta associated with the menu item), so you have to also merge those into the returned array if you want to use them