How To Prevent Category Widget From Using Category Description As Title Attribute

The Categories widget in WordPress uses the category description as the title attribute for the list items displayed by the widget. While this may be great for very short descriptions, it doesn’t make sense when longer descriptions are in use.

Here’s an example of a medium size description which is diplayed on mouseover:

screenshot showing a long category description as the title attribute on mouseover

Ugly. There is no way to turn this off in the widget options, but it can be disabled by dropping some code into functions.php.

Why Use Long Category Descriptions?

Many themes display the category description on the Category archive pages. This can be useful for users, as it sets the context of the page rather than just serving them up a long list of posts. It’s also useful for SEO, as search engines are likely to rank category pages higher if they include some unique content.

I use long category descriptions for the reasons above, but also because my category pages are critical part of my approach to applying a silo structure to WordPress.

If you’re going to display the category description on the front end, then it needs to be longer than would be sensible for a title attribute.

HTML In Category Descriptions

Complicating matters, the category description allows some basic HTML (such as the strong tag and links). People who are displaying category descriptions on the front end often make use of this. Many even use a plugin such as Allow HTML in Category Descriptions by Arno Esterhuizen to allow other HTML (such as heading tags).

Functionality to allow HTML in category descriptions is also built into WordPress SEO by Yoast meaning many people have this functionality without realising it – not that it matters unless people actually put HTML in their category descriptions.

Having HTML within the title attribute is not a good idea! Not only is not semantic, we don’t know how it will be handled by:

  • assistive technologies for disabled persons, such as screen readers
  • search engines

While it probably isn’t the end of the world, we don’t want HTML in the title attributes.

Solution – The widget_categories_args Filter

Fortunately, we can use the widget_categories_args filter to change this. Simply add the following code to your functions.php (or functionality plugin):

// prevent the category widget from using the category description as the list item title attribute
function sjc_disable_cat_desc_widget_list_titles ( $cat_args ) {
	$cat_args[ 'use_desc_for_title' ] = 0;
	return $cat_args;
}
add_filter( 'widget_categories_args', 'sjc_disable_cat_desc_widget_list_titles' );
The widget output is created using wp_list_categories, so you could use this technique to change any of the parameters accepted by wp_list_categories.

Remember to make a copy your functions.php file before adding the code, so you can copy it back if something goes wrong!

The Result

By disabling the use of category descriptions, the title attribute returns to “View all posts filed under categoryname”, which is what’s used if there is no category description:

screenshot showing the title attribute when the category description has been disabled

Not as good as a purpose written title attribute, but far better than having War and Peace as the title!

One response on “How To Prevent Category Widget From Using Category Description As Title Attribute

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Quiz: