0

I've been using the nav_menu_css_class filter in one of my plugins, implemented as:

add_filter( 'nav_menu_css_class', 'wbwcrf_nav_menu_css_class', 10, 3 );
function wbwcrf_nav_menu_css_class( $classes, $item, $args )
{
    /* snip */
    return $classes;
}

The filter clearly has 3 arguments in the code (source) yet someone using my plugin has told me they get the error Warning: Missing argument 3 for wbwcrf_nav_menu_css_class().

So I looked at the codex documentation on nav_menu_css_class which has an example using 2 arguments, I changed my code to 2 arguments since I'm not actually using the $args argument and everything seems to be working fine.

But I'm still really stumped as to why the source has 3 arguments yet the codex has 2 and why I don't receive any error when using 3 arguments, yet someone using my plugin does. Anyone able to clear up my confusion here please?

1 Answer 1

3

I can confirm that this filter applied with 3 parameters as you mentioned in your snippet. The warning is very strange and shouldn't appear if WordPress core wan't hacked.

The best practice for this case is to request parameters, which are required by your hook. So if you don't use $args, so don't request it:

add_filter( 'nav_menu_css_class', 'wbwcrf_nav_menu_css_class', 10, 2 );
function wbwcrf_nav_menu_css_class( $classes, $item ) 
{
    // ...
}

Another approach is to set default values for income arguments:

add_filter( 'nav_menu_css_class', 'wbwcrf_nav_menu_css_class', 10, 3 );
function wbwcrf_nav_menu_css_class( $classes, $item, $args = array() ) 
{
    // ...
}

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.