Removing title attributes from WordPress links

One of the things that annoyed me when I started customising WordPress is that it likes put a title attribute on just about every link it generates. There are several reasons I don’t like that:

So the title attribute should be used with care, not just routinely added to every link.

I’m not alone in wanting to remove the title attributes from WordPress – there is even a plugin called Remove Title Attributes. Using that plugin is an option of course, but if you’re like me and want to keep your WordPress installs as plugin-free as possible, you can clean out the title attributes yourself by adding the following to your functions.php file:

function remove_title_attributes($input) {
	return preg_replace('/\s*title\s*=\s*(["\']).*?\1/', '', $input);
add_filter( 'wp_list_pages', 'remove_title_attributes' );

This little regular expression looks for title attributes whose values can be surrounded by either single or double quotes and deletes them. It isn’t foolproof as it will have problems if an attribute value contains escaped quotes of the same type as the opening quote. That said, I haven’t seen it trip on any of the title attributes produced by WordPress yet. Just something to be aware of if you notice something weird when using this regexp though.

The simplest way to use it is to add it as a filter to the WordPress function that you want to remove title attributes from, like wp_list_pages in the example.

Bye bye, title attributes :-).

Posted on November 2, 2010 in WordPress, Accessibility