Tricks

[Solved] How to Remove WordPress Menu Class & ID

Some of you may want to remove wp_nav_menu remove classes from li

Just like this:

<div id="navi">
			<div class="menu-primary-container"><ul id="menu-primary" class="menu"><li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-14"><a href="http://localhost/2in1design/intro-2/">Intro</a></li>
<li id="menu-item-16" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16"><a href="http://localhost/2in1design/category/portfolio/">Portfolio</a></li>
<li id="menu-item-20" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20"><a href="http://localhost/2in1design/contact/">Contact</a></li>
<li id="menu-item-15" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-15"><a href="http://localhost/2in1design/category/blog/">Blog</a></li>
<li id="menu-item-17" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-17"><a href="#">Shop</a></li>

</ul></div>        </div>

If you don’t know how to remove the class and id, here is the solution.

I used the ideas on stackoverflow here, but it didn’t work for me, I use the following code, and it solved the problem!

Copy and paste the following code in your functions.php file (you can find under the theme’s folder.)

function current_to_active($text){

        $replace = array(

                //List of menu item classes that should be changed to "active"

                'current-menu-item' => 'active',

                'current_page_parent' => 'active',

                'current_page_ancestor' => 'active',

        );

        $text = str_replace(array_keys($replace), $replace, $text);

                return $text;

        }

add_filter('nav_menu_css_class', 'current_to_active');

add_filter('nav_menu_item_id', 'current_to_active');

add_filter('page_css_class', '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');