0

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?!

  • So you want to return an array that contains only items that begin with fa- or appear in a whitelist? Is there anything specific to WordPress that requires WordPress expertise here and not generic PHP array knowledge? – Tom J Nowell Aug 30 '14 at 2:03
  • Yes, that would do the trick - an array that contains items in a whitelist (as in my current function) as well as items beginning with fa-. I dunno, is this more of a PHP question than WordPress? – GermanKiwi Aug 30 '14 at 10:10
0

array_intersect() is not a good choice when you need a partial match. Go through each class, and apply both checks before you remove a class.

Example, not tested:

function remove_menu_classes( $classes ) {

    // List of allowed menu classes
    $allowed =  array (
        'current-menu-item',
        'current-menu-ancestor',
        'menu-item-has-children',
        'current-post-ancestor',
        'first',
        'last',
        'vertical',
        'horizontal'
    );

    if ( ! is_array( $classes ) )
        return $classes;

    foreach ( $classes as $key => $class ) {

        // keep allowed classes
        if ( in_array( $class, $allowed ) )
            continue;

        // keep font awesome classes
        if ( 0 === strpos( $class, 'fa-' ) )
            continue;

        // remove the rest
        unset ( $classes[ $key ] );
    }

    return $classes;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.