Recently while working on one of the project, I needed to remove the title attribute of WordPress generated menu item.

It was a child theme and menu was already generated by the parent theme.

So to fix this issue, I had to write a small filter to remove the generated code. To remove the title attribute from WordPress menu item, we can use “nav_menu_link_attributes” filter.

nav_menu_link_attributes” filters the HTML attributes applied to a menu item’s anchor element.

Are you facing “jQuery Is Not Defined WordPress” Issue?

Check how to fix wordpress jquery is not defined issue.

This filter runs per nav item. This is used to add, modify or delete HTML attributes of WordPress menus nav item, generated with the WP Menu API.

Wordpress filter to add, modify, delete attributes of wordpress generated menu item link. How to modify attributes of any wordpress menu item link. Remove Title Attribute from WordPress Navigation Menu Item Link.
Remove Title Attribute from WordPress Navigation Menu Item Link

Here is the WordPress filter to remove/modify link title attribute of menu nav item.

add_filter( 'nav_menu_link_attributes', 'remove_title_from_menu', 10, 4 );
function remove_title_from_menu( $atts, $item, $args, $depth ){
	unset($atts['title']);
	return $atts;
}

Similarly, say if you want to add a custom HTML attribute or if you want to add HTML Data attributes to links, then you can use the same filter.

Here is a sample code to do the same

add_filter( 'nav_menu_link_attributes', 'add_data_attribute_menu', 10, 4 );
function add_data_attribute_menu( $atts, $item, $args, $depth ){
	$atts['data-info'] = 'Custom '.$atts['title'].' information';
	return $atts;
}

Also, if you want to add custom text for all links which are opening in new tab eg. external link. Then you can use this function to add custom class to WordPress navigation menu item.

If you want to know more about this filter, then here is a link to nav_menu_link_attributes documentation.

Is it useful? Don’t forget to share it with your friends & colleagues.

Did you use this WordPress filter? What was your use case? Let me know in comments.

Checkout other WordPress related articles.