Remove Redundant Title Attributes From Menu Links
Strips title attributes from menu links when they only repeat the visible link text, which makes screen readers announce the same words twice.
Opens WPIDE on
Code
add_filter( 'nav_menu_link_attributes', function ( $atts, $item ) {
if ( empty( $atts['title'] ) ) {
return $atts;
}
$text = strtolower( trim( wp_strip_all_tags( $item->title ) ) );
if ( strtolower( trim( $atts['title'] ) ) === $text ) {
unset( $atts['title'] );
}
return $atts;
}, 10, 2 );
Notes
Titles that add new information are kept; only exact repeats of the link text are removed.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.