annoying Category

Removing title tags from wordpress menus

April 7th, 2009

I love wordpress, I really do. But it has a couple of annoying hitches that I hope will be fixed in V3.0. One of those hitches is the way wordpress displays a ‘little yellow popup’ caused by the title attribute on linked items. This title tag is designed to increase accessibility as encouraged by the Web Accessibility Initiative. I strongly believe in making the web as accessible as possible, but there comes a point where things like a persistant title bubble make it even more unusable.

Because this function is hardcoded deep inside wordpress’s code, it’s actually quite confusing to work out how to disable it. Luckily I found a brilliant post that outlined a solution. It uses the php function preg_replace to exclude the title tag from being rendered in the menus. It works almost perfectly except it also renders out all the other arguments that are usually passed to wordpress to customise your menu output. For example if your code looked like this:

<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>

wordpress would remove the title tag but also ignore “title_li=&child_of=”. It took a bit of thinking, but here is one solution to this problem:

<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
$children = preg_replace('/title="(.*?)"/','',$children);?>