Using WordPress ‘nav_menu_attr_title’ PHP filter

The nav_menu_attr_title WordPress PHP filter allows you to modify a navigation menu item’s title attribute.

Usage

add_filter( 'nav_menu_attr_title', 'custom_nav_menu_attr_title' );

function custom_nav_menu_attr_title( $item_title ) {
  // your custom code here
  return $item_title;
}

Parameters

  • $item_title (string): The menu item title attribute.

More information

See WordPress Developer Resources: nav_menu_attr_title

Examples

Add a prefix to the menu item title attribute

This example adds a prefix “Click here: ” to the menu item title attribute.

function prefix_nav_menu_attr_title( $item_title ) {
  return 'Click here: ' . $item_title;
}
add_filter( 'nav_menu_attr_title', 'prefix_nav_menu_attr_title' );

Capitalize the menu item title attribute

This example capitalizes the menu item title attribute.

function capitalize_nav_menu_attr_title( $item_title ) {
  return strtoupper( $item_title );
}
add_filter( 'nav_menu_attr_title', 'capitalize_nav_menu_attr_title' );

Replace specific word in the menu item title attribute

This example replaces the word “Home” with “Main” in the menu item title attribute.

function replace_word_nav_menu_attr_title( $item_title ) {
  return str_replace( 'Home', 'Main', $item_title );
}
add_filter( 'nav_menu_attr_title', 'replace_word_nav_menu_attr_title' );

Add a custom title attribute to a specific menu item

This example sets a custom title attribute “Custom Title” to a menu item with an ID of 42.

function custom_title_nav_menu_attr_title( $item_title, $item ) {
  if ( $item->ID == 42 ) {
    return 'Custom Title';
  }
  return $item_title;
}
add_filter( 'nav_menu_attr_title', 'custom_title_nav_menu_attr_title', 10, 2 );

Remove the title attribute from all menu items

This example removes the title attribute from all menu items.

function remove_nav_menu_attr_title( $item_title ) {
  return '';
}
add_filter( 'nav_menu_attr_title', 'remove_nav_menu_attr_title' );