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

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


Parameters

$atts array
The HTML attributes applied to the menu item's <a> element, empty strings are ignored.
  • titlestring
    Title attribute.
  • targetstring
    Target attribute.
  • relstring
    The rel attribute.
  • hrefstring
    The href attribute.
  • aria-currentstring
    The aria-current attribute.
$menu_item WP_Post
The current menu item object.
$args stdClass
An object of wp_nav_menu() arguments.
More Arguments from wp_nav_menu( ... $args ) Array of nav menu arguments.
  • menuint|string|WP_Term
    Desired menu. Accepts a menu ID, slug, name, or object.
  • menu_classstring
    CSS class to use for the ul element which forms the menu.
    Default 'menu'.
  • menu_idstring
    The ID that is applied to the ul element which forms the menu.
    Default is the menu slug, incremented.
  • containerstring
    Whether to wrap the ul, and what to wrap it with.
    Default 'div'.
  • container_classstring
    Class that is applied to the container.
    Default 'menu-{menu slug}-container'.
  • container_idstring
    The ID that is applied to the container.
  • container_aria_labelstring
    The aria-label attribute that is applied to the container when it's a nav element.
  • fallback_cbcallable|false
    If the menu doesn't exist, a callback function will fire.
    Default is 'wp_page_menu'. Set to false for no fallback.
  • beforestring
    Text before the link markup.
  • afterstring
    Text after the link markup.
  • link_beforestring
    Text before the link text.
  • link_afterstring
    Text after the link text.
  • echobool
    Whether to echo the menu or return it. Default true.
  • depthint
    How many levels of the hierarchy are to be included.
    0 means all. Default 0.
    Default 0.
  • walkerobject
    Instance of a custom walker class.
  • theme_locationstring
    Theme location to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
  • items_wrapstring
    How the list items should be wrapped. Uses printf() format with numbered placeholders. Default is a ul with an id and class.
  • item_spacingstring
    Whether to preserve whitespace within the menu's HTML.
    Accepts 'preserve' or 'discard'. Default 'preserve'.
$depth int
Depth of menu item. Used for padding.

Top ↑

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

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

$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );


Top ↑

Changelog

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

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Steven Lin

    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.

  2. Skip to note 2 content
    Contributed by Steven Lin

    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 );
  3. Skip to note 3 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.

  4. Skip to note 5 content
    Contributed by Steven Lin

    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 );
  5. Skip to note 6 content
    Contributed by Muhammad Ayoub

    I have solution to add class to anchor tag.

    1: Step: add this in functions.php

    function add_additional_class_on_a($classes, $item, $args)
    {
    	if (isset($args->add_a_class)) {
    		$classes['class'] = $args->add_a_class;
    	}
    	return $classes;
    }
    
    add_filter('nav_menu_link_attributes', 'add_additional_class_on_a', 1, 3);

    2: Then use it like this

    <?php
    		// Show Menu here
    		wp_nav_menu(array(
    			'theme_location' => 'my-footer-menu',
    			'container_id'    => '',
    			'menu_class'      => 'footer-top list-unstyled',
    			'menu_id'         => '',
    			'add_a_class'     => 'box-link text-dark',
    		));
    ?>

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