php – Providin exception to WordPress wp_nav_menu Custom CSS Classes


I use below code to remove unusued menu classes.

function clear_nav_menu_item_class($var) {
    return is_array($var) ? array_intersect($var, array('menu-item-has-children')) : '';
add_filter( 'nav_menu_css_class', 'clear_nav_menu_item_class' );

It removes all classes except for “menu-item-has-children” but it also removes a Class I add to the menu optionally from (CSS Classes) section

How can I add an exception for it not to remove the CSS Classes I added from here:

enter image description here

Before the code is run:

enter image description here

When the code is run:

enter image description here

What I want:

enter image description here

Note: I added “custom-class-name” as an example. The Class name to be added can be anything. It can not be foreseen.

Murat Dinc 2 years 2021-11-18T08:42:43-05:00 0 Answers 0 views 0

Leave an answer