apply_filters( 'nav_menu_link_attributes', array $atts, WP_Post $item, stdClass $args, int $depth )

Filters the HTML attributes applied to a menu item’s anchor element.


Parameters Parameters

$atts

(array) The HTML attributes applied to the menu item's <a> element, empty strings are ignored.

  • 'title'
    (string) Title attribute.
  • 'target'
    (string) Target attribute.
  • 'rel'
    (string) The rel attribute.
  • 'href'
    (string) The href attribute.
  • 'aria_current'
    (string) The aria-current attribute.

$item

(WP_Post) The current menu item.

$args

(stdClass) An object of wp_nav_menu() arguments.

$depth

(int) Depth of menu item. Used for padding.


Top ↑

More Information More Information

  • The filter permits full control over what HTML attributes are added to menus generated with the WP Menu API.
  • This filter runs per nav item, vs providing a list of all nav elements at once.
  • Note that the callback function must return a value after it is finished processing or the result will be empty.

Top ↑

Source Source

File: wp-includes/class-walker-nav-menu.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
4.1.0 The $depth parameter was added.
3.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Justin Kopepasah

    The default Walker menu does not include a class attribute on the anchor (<a>) element, but one can easily be added using this filter.

    function add_class_to_all_menu_anchors( $atts ) {
    	$atts['class'] = 'menu-item-anchor';
    
        return $atts;
    }
    add_filter( 'nav_menu_link_attributes', 'add_class_to_all_menu_anchors', 10 );
    

    You might notice there that I am only passing one parameter ($atts) to the function. This is because we only need the $atts in this case and there is no need to pass other parameters. Alternatively, let’s say we wanted to add a class to all anchors which are not top level:

    function add_class_to_non_top_level_menu_anchors( $atts, $item, $args, $depth ) {
    	if ( 0 !== $depth ) {
    		$atts['class'] = 'menu-item-anchor-non-top';
    	}
    
    	return $atts;
    }
    add_filter( 'nav_menu_link_attributes', 'add_class_to_non_top_level_menu_anchors', 10, 4 );
    

    In this case, we need to pass all four parameters because we are using the $depth parameter.

  2. Skip to note 3 content
    Contributed by stevenlinx

    Example migrated from Codex:

    The following example adds an attribute to specific menu items (34 and 39). Specify the ID of each menu item as an array.

    function add_specific_menu_atts( $atts, $item, $args ) {
    	$menu_items = array(34,39);
    	if (in_array($item->ID, $menu_items)) {
    	  $atts['onClick'] = 'return false';
    	}
    	
        return $atts;
    }
    add_filter( 'nav_menu_link_attributes', 'add_specific_menu_atts', 10, 3 );
    

    In the simplest instance above, the filter adds the attribute to all menu items of all menus. In case you only want to add the attributes to a certain menu location, you can check for menu location in a conditional.

  3. Skip to note 4 content
    Contributed by stevenlinx

    Example migrated from Codex:

    The following function adds a class attribute to all <a> tags in a particular menu location (‘primary’).

    function add_specific_menu_location_atts( $atts, $item, $args ) {
        // check if the item is in the primary menu
        if( $args->theme_location == 'primary' ) {
          // add the desired attributes:
          $atts['class'] = 'menu-link-class';
        }
        return $atts;
    }
    add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );
    
  4. Skip to note 5 content
    Contributed by stevenlinx

    Example migrated from Codex:

    The following function adds a class to all menu items that have “Open link in a new window/tab” checked in the menu options.

    function add_class_to_target_blank_menu_items( $atts, $item, $args ) {
        // check if the item is set to target="_blank"
        if ( $item->target == '_blank' ) {
          // add the desired attributes:
          $atts['class'] = 'target-blank';
        }
        return $atts;
    }
    add_filter( 'nav_menu_link_attributes', 'add_class_to_target_blank_menu_items', 10, 3 );
    

You must log in before being able to contribute a note or feedback.