I'm currently using a function which strips out all the unwanted classes from my nav menus, leaving only the classes I want to keep (the ones listed in the array inside the function). It looks like this:
function remove_menu_classes($var) {
return is_array($var) ? array_intersect($var, array (
// List of allowed menu classes
'current-menu-item',
'current-menu-ancestor',
'menu-item-has-children',
'current-post-ancestor',
'first',
'last',
'vertical',
'horizontal'
)
) : '';
}
add_filter( 'nav_menu_css_class', 'remove_menu_classes' );
add_filter( 'nav_menu_item_id', 'remove_menu_classes' );
add_filter( 'page_css_class', 'remove_menu_classes' );
Now, I want to add some additional classes to some of my menu items, using the "CSS Classes" field of each menu item, in order to add Font Awesome icons next to the menus. This is accomplished using a plugin.
All I need to do is add (for example) "fa-home" into the CSS Classes field, or whatever Font Awesome class I want to use. The plugin checks for any CSS class starting with fa-
that exists within the nav <li>
tags, and if they exist, the plugin adds the appropriate <i class="fa-whatever">
tag inside the <li>
tag for me, which displays the icon. Very handy.
The problem is that my function above strips these fa-
classes out before the plugin is able to see them and act on them. I've tried changing the priorities of the "add_filter" lines in the function (to both '1' and '999') but that didn't help.
So I'm wondering if there's any way to modify my function above, so that it will not strip out any classes that begin with fa-
. I don't think I can use wildcards in an array like that, but my knowledge of PHP is not advanced enough to know if there are any other ways of writing this function that will achieve this goal (maybe using a completely different method for example). I'd prefer to keep it as simple as possible though, preferably without using a walker. :)
Any ideas?!