title
attribute on just about every link it generates. There are several reasons I don’t like that:- In most cases there is no option to disable the unwanted title attributes.
- Many of the
title
attributes simply repeat the link text, which is meaningless – see Don’t duplicate link text in the title attribute for more info on why. - Unless used with care,
title
attributes can be annoying or confusing to screen reader users. When screen readers encounter a link with atitle
attribute, they may read thetitle
attribute instead of the link text, before the link text, after the link text, or not at all. It depends on which screen reader it is, how it is configured, and the content of the title text. Addingtitle
attributes to every link does not improve accessibility – it can have a negative impact. On top of that, title text is unavailable to some users – see Don’t use the title attribute for essential information for more info on 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 :-).
No comments:
Post a Comment