Note: This is more of a tutorial/wiki than a real question and should be a reference for later Questions. If you got something to add, please feel free to add an answer. Working answers get upvoted. :)


You want to modify the output of some wp core function and instead of modifying the core directly (which is always bad), you have found a call like this:

$filterable_var = apply_filters( 'name_of_filter', $filterable_var );

The filter function allows you to modify eg. $filterable_var and therefore the output of the wp function.

The Problem

You don't know what exactly the function wants as input.

The Tags

Check what you can modify.

Possibly Related Q:
[Fell free to add your own Questions here.]

  • @MikeSchinkel @Rarst I think this should be a community wiki
    – kaiser
    May 26, 2011 at 15:57

1 Answer 1



Nav menu walker - allows adding eg. css classes to (all) menu items.

// copyied from /wp-core/wp-includes/nav-menu-template.php > line 76 (wp 3.1.1) - start_el() function
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );

Now let's check the $var:

function wpse15319_check_nav_menu_classes( $classes ) 
    // You can take any of the $vars from the core function above: $classes, $item, $args
    echo '<pre>';
        // nice list:
        print_r( $classes ); 
        // or check of what type the $var is
        var_dump( $classes );
    echo '</pre>';

    return $classes;
add_filter( 'nav_menu_css_class', 'wpse15319_check_nav_menu_classes', 10 );

Modify - Applied example

function wpse15319_add_nav_menu_classes( $classes ) 
    $classes[] = '';
    $classes[] .= 'my added css classes'; 

    return $classes;
add_filter( 'nav_menu_css_class', 'wpse15319_add_nav_menu_classes', 10 );
  • 1
    nice but what do you do when you don't have apply_filters hooks and don't want to modify the core files???
    – Bainternet
    Apr 21, 2011 at 14:44
  • @Bainternet - open a new Q here on WA and give an answer to that ,) - (and link to that at the bottom of the Q)
    – kaiser
    Apr 21, 2011 at 14:57
  • if only i knew the answer... :)
    – Bainternet
    Apr 21, 2011 at 14:59
  • @Bainternet: easy ... fork, mod & redistribute wp ,)
    – kaiser
    Apr 21, 2011 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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