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.

Description Description

Parameters Parameters


(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.


(WP_Post) The current menu item.


(stdClass) An object of wp_nav_menu() arguments.


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

Top ↑

Source Source

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

View on Trac

Top ↑

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.

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