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:


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