Remove title attributes from WordPress menu wp_nav_menu

If you’re reading this then you are looking for a solution to remove those annoying title attributes from menus generated by WordPress built-in functions wp_nav_menu(), wp_page_menu(), wp_list_categories(). Some might argue against removing those because of the accessibility issues it might introduce, but in most cases those annoying tooltips that are being displayed by browsers when the title attribute is found usually look rather bad and we want to keep out navigation clean, don’t we?

The solution is quite simple. We will utilize regular expressions to do that. Don’t worry if you’re not familiar with them, just follow instructions below and it will work.

Simply edit your theme’s functions.php and add the code below to it

function remove_title_attr( $nav )
{
return $nav = preg_replace('/ title=\"(.*?)\"/', '', $nav );
}

add_filter( 'wp_nav_menu', 'remove_title_attre' );
add_filter( 'wp_page_menu', 'remove_title_attr' );
add_filter( 'wp_list_categories', 'remove_title_attr' );

You don’t have to add filters to all of the functions, just leave one/ones you use.

Leave a comment