X

Remove every class and id from the wp_nav_menu

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.



Code Snippet:


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')) : '';
}

Comments  Leave a Reply

  1. thanks. but if there have custom css class then i want show that too. but rid of all other default class. any idea how

  2. 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

  3. 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

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

  5. Electronics News October 7, 2011 at 11:03 am

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

    1. Cool glad to help.

      1. 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

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!