PHP Fatal Error using nav_menu_link_attributes filter

Hi Folks!

I’m working with the nav_menu_link_attributes filter to add a custom data-toggle attribute assigned from a ACF field to a menu-item link, but am encountering a PHP fatal error. For some reason, inclusion of the $item parameter is causing the following error:

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Too few arguments to function App\{closure}(), 2 passed in /app/wp-includes/class-wp-hook.php on line 288 and exactly 3 expected

Passing $atts['data-toggle'] = 'foo' without the $item param works fine. Though $args->theme_location == 'primary_navigation' doesn’t seem to exclude the other navigation.

The sample code is below:

add_filter('nav_menu_link_attributes', function( $atts, $item, $args ) {

  if($args->theme_location == 'primary_navigation')
  
  $data_value = esc_html( get_field('data_attribute_value', $item) );
  $atts['data-toggle'] = $data_value;

  return $atts;

}, 10, 3);

I would appreciate any insights, from what I can tell, the $item param isn’t being passed to the function, but I’m not sure why?

Ah, never mind. Figured it out! A simple mistake. Didn’t update the $accepted_arguments param while I was testing.

This topic was automatically closed after 42 days. New replies are no longer allowed.