X

Remove Every Class and ID from the wp_nav_menu

Snippets by IsItWP

Do you want to remove all unwanted classes and IDs from the WordPress navigation menu? There can be many classes and IDs that may conflict with your required styling. We have created a quick code snippet that you can use to remove every class and ID from the wp_nav_menu except for the current-menu-item class.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: How to create a contact form on WordPress and 27 best WordPress business themes for your website.

Comments   Leave a Reply

  1. How if i want to replace current-menu-item to active? thx

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

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

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

  6. 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!