nav_menu_link_attributes
Allows changing the <a> HTML attributes of a menu item: title, target, rel, and href.
By default, these attributes are configured in the menu editor in the admin panel:
The href attribute is the link. It is assigned automatically based on the selected item (post, category, and so on), or entered manually when a Custom Link item is selected.
Usage
add_filter( 'nav_menu_link_attributes', 'wp_kama_nav_menu_link_attributes_filter', 10, 4 );
/**
* Function for `nav_menu_link_attributes` filter-hook.
*
* @param array $atts The HTML attributes applied to the menu item's `<a>` element, empty
* strings are ignored.
* @param WP_Post $menu_item The current menu item object.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*
* @return array
*/
function wp_kama_nav_menu_link_attributes_filter( $atts, $menu_item, $args, $depth ){
// filter...
return $atts;
}
- $atts(array)
HTML attributes for the menu item's
<a>element. Attributes with empty values are not output.-
$title(string)
Thetitleattribute. For example, "This item is very important." -
$target(string)
Thetargetattribute. For example,_blank. -
$rel(string)
Therelattribute. For example,nofollow. - $href(string)
Thehrefattribute.
-
- $item(WP_Post)
The current menu item object. Example:
- $args(stdClass)
An object containing the arguments from wp_nav_menu(). Example:
- $depth(int)
- The menu item depth. Added in version 4.1.0 and used for indentation. Top-level menu items have
$depth = 0, their children have$depth = 1, and so on.
Examples
#1 Open external links in a separate tab
add_filter( 'nav_menu_link_attributes', 'change_nav_menu_link_attributes' );
function change_nav_menu_link_attributes( $atts ) {
if ( strpos( $atts['href'], home_url() ) === false ) {
$atts['target'] = '_blank';
}
return $atts;
}
#2 Add a class attribute to the active link
This example shows how you can extend the list of classes for a WordPress menu item and add your own class to the current active menu element.
// Add classes to links
add_filter( 'nav_menu_link_attributes', 'filter_nav_menu_link_attributes', 10, 4 );
function filter_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
if ( $item->current ) {
$class = 'menu-link--active';
$atts['class'] = isset( $atts['class'] ) ? "{$atts['class']} $class" : $class;
}
return $atts;
}
#3 Make all menu links relative
Remember that links to external sites must not be passed through wp_make_link_relative().
add_filter( 'nav_menu_link_attributes', 'change_nav_menu_atts', 10, 2 );
function change_nav_menu_atts( $atts, $item ) {
$atts['href'] = str_replace( home_url(), '', $item->url );
return $atts;
}
#4 Add attributes to links for opening a popup when clicked
Suppose a JavaScript script monitors links with the js-open-modal class. When one is clicked, the script reads its data-modal attribute and opens the popup with that number.
/**
* Adds a class and popup number to menu links so the popup opens when clicked.
* Assign modal-window-1 to a menu item in the admin panel to open popup #1.
* Change the number to select a different popup.
*/
add_filter( 'nav_menu_link_attributes', function ( $atts, $menu_item ) {
if ( empty( $menu_item->classes ) ) {
return $atts;
}
foreach ( $menu_item->classes as $class ) {
if ( strpos( $class, 'modal-window-' ) !== false ) {
if ( isset( $atts['class'] ) ) {
$atts['class'] .= ' js-open-modal';
} else {
$atts['class'] = 'js-open-modal';
}
$atts['data-modal'] = preg_replace( '/[^0-9]/', '', $class );
break;
}
}
return $atts;
}, 11, 2 );Changelog
| Since 3.6.0 | Introduced. |
| Since 4.1.0 | The $depth parameter was added. |
Where the hook is called
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
