[resolved] nav_menu_css_class Missing argument 2 (3 posts)

  1. scibuff
    Posted 4 years ago #

    I'm trying to add some classes to my menu items... I see that the Walker_Nav_Menu calls apply_filters with the 'nav_menu_css_class' tag and two arguments ( $classes and $item )
    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );

    Nevertheless, filter function receives only one argument, i.e. only the $classes array

    function my_nav_menu_css_class( $classes = array(), $item ) {
    	echo "\n<!-- func_get_args: \n"; print_r( func_get_args() ); echo "\n-->\n";
    	return $classes;
    add_filter( 'nav_menu_css_class', 'my_nav_menu_css_class' );

    keeping the second argument in my function results in "missing argument 2" warning and the value of $item is NULL

    ... thus to achieve a simple task of adding classes I have to use a Walker class (which works fine) but it should NOT need to be necessary

  2. Mark / t31os
    Posted 4 years ago #

    By default filters/actions support only one parameter/arg, you need to specify the supported args inside the add_filter call with the fourth parameter..

    add_filter( 'nav_menu_css_class', 'my_nav_menu_css_class', 10, 2 );

    10 - priority
    2 - supported args

    This is covered on the docs for add_filter.

  3. scibuff
    Posted 4 years ago #


Topic Closed

This topic has been closed to new replies.

About this Topic