Remove the Title Attribute from WordPress Category and Page Lists

Posted on March 11, 2009  |  Category: Tutorials

I was creating a drop down menu for the categories in my Magazine Basic theme and I came across something that started to annoy me a bit. When you hover over the menu item, and the sub-menu dropped down, a title attribute would appear and cover some of the menu. This title attribute it hard wired into WordPress. Just hover over any menu item above and you will see a yellow box appear that says something like “View all posts files under…”.

This is a useful attribute to add to any site in regards to the Web Accessibility Initiative, but if it interferes with the basic navigation of your site, I think it is okay to nix it.

Here is a simple pieces of code that you can use to remove the title attribute from your category list:

<?php
$categories = wp_list_categories('echo=0');
$categories = preg_replace('/title=\"(.*?)\"/','',$categories);
echo $categories;
?>

If you really want to get fancy, you can add this as a function to your theme’s function.php file. If the file doesn’t exist, just create it.

Add this:

<?php
function categories_without_title_attribute() {
$categories = wp_list_categories('echo=0');
$categories = preg_replace('/title=\"(.*?)\"/','',$categories);
echo $categories;
}
?>

Now when you want to call your category list, use <?php categories_without_title_attribute(); ?> instead of <?php wp_list_categories(); ?>.

You can do the same to your page lists, just replace wp_list_categories('echo=0') with wp_list_pages('echo=0').


Tags: , , , , , , , , , , ,

Short URL: http://bavotasan.com/?p=359

20 Responses to “Remove the Title Attribute from WordPress Category and Page Lists”

  1. Tim

    Hello,

    Thanks for this. The example works great for removing the title tag from categories, but I just can’t get it to work for page lists.

    Could you please post the php code used to remove title tags from wp_list_pages();

    This is what I tried.

    function pages_without_title_attribute() {
    $notitlepages = wp_list_pages(‘echo=0′);
    $notitlepages = preg_replace(‘/title=\”(.*?)\”/’,”,$notitlepages);
    echo $notitlepages;

    The moment this code goes into my functions.php file it crashes wordpress (whether I am calling it or not). I have tried a few different variable names but I cant get this to work.

    I am using Wordpress 2.7.

    Thanks
    Tim

    #1565
  2. Tim

    Never mind… worked it out.

    Was missing the end }.

    Thanks again. Works perfectly.

    Tim

    #1566
  3. Probably the best solution anyone’s come up with. However, this function is not similar to wp_list_pages in that it does not accept all of the attributes of the native WP function.
    For example:

    #1977
  4. Here is the code snippet for the example in the previous post:
    wp_list_pages(‘title_li=&sort_column=menu_order&depth=1&child_of=144′);

    Dreaming of a solution where I’d be able to add all those attributes in place while also stripping the “<a title=” attribute.

    #1978
  5. Thanks for this, I hate “redundant title attributes”. Not needed for web accessibility or anything else. (When the title is the same as the link text.)

    #3056
  6. Jeff Federman

    Thanks for the tip, the title tool tips can be distracting. Here’s a little modification I made, in case you want to use some of the wplistpages parameters (e.g., ‘childof’, ’sortcolumn’, etc.):

    function listPagesNoTitle($args) {
    if ($args) {
    $args .= '&echo=0';
    } else {
    $args = 'echo=0';
    }
    $pages = wp_list_pages($args);
    $pages = preg_replace('/title=\"(.*?)\"/', '', $pages);
    echo $pages;
    }

    #3461
  7. Rather than removing the default hover text, how can I go about changing it?

    #3593
    • Oh. That would take some hacking. Not sure which file you would have to go at though. WordPress by default just pulls of the Cat title or the Cat description.

      #3633
    • b.parks

      Watt, I found this on another site. You have to go to the wp-includes folder and change the text string(s) in the classes.php and category-template.php files.

      In the category-template.php file you have to change it in 6-7 different places.

      I don’t like changing the core wp files, so the above works better for me (if I could get it to work, that is).

      #4131
    • Donny

      It’s easy!
      Using Jeff’s code above, just replace ‘title=’ with ‘alt=’ in ‘preg_replace’, e.g.:

      function pages_with_alt_attribute($args) {
          if ($args) {
      	$args .= '&amp;echo=0';
          } else {
      	$args = 'echo=0';
          }
          $pages = wp_list_pages($args);
          $pages = preg_replace('/title=/', 'alt=', $pages);
          echo $pages;
      }

      This is better than removing the ‘title’ attr. completely from accessibility point of view. Screen readers can still read ‘alt’ attributes.

      #5830
  8. Karar.A.

    very good :)

    there is another way to do this with jQuery > just add this code to your functions.php file in you theme:

    function remove_alt(){
    wp_enqueue_script(‘jquery’);
    ?>

    jQuery(document).ready(function(){
    jQuery(“li.cat-item a”).each(function(){
    jQuery(this).removeAttr(‘title’);
    })
    jQuery(“li.page_item a”).each(function(){
    jQuery(this).removeAttr(‘title’);
    })

    });

    <?php
    }
    add_filter('wp_head','remove_alt');

    save it and it will work “remember you need to add jquery in your theme”

    Thank you again :)

    #5201
  9. What about replacing ‘the_title’ with ‘the_title_attribute’ BUT only in sidebars or active widgets? Would you know how to do this?

    I wish I were a coder, but, I’m not and cannot find this ANYWHERE. I even tried the WP forums to no avail. Looking at your example makes me thing you might know how to do this.

    Thanks in advance. Hoping you might know!

    #5885
  10. Djor

    I already use a preg replace to add span to the links. Does anyone know how I can combine both codes?

    <?php 
    	echo preg_replace('@\]*)>\<a>]*)>(.*?)\@i', '<a>$3</a>',wp_list_categories('echo=0&orderby=id&title_li=&depth=1&include=14')); 
    ?>
    #6371
  11. Those who aren’t comfortable editing their theme files can use the Remove Title Attributes plugin to solve this problem.

    #6979
  12. Thanks, this was exactly what I was searching for. It was terribly annoying.

    #6996
  13. Guy

    Is there nothing you can tick in wordpress, I cant imagine trying to mess with the pages. Last time I did that I messed it up

    #7486
  14. No its annoying but I think you can only do it with code.

    #7487
  15. If you have filled descriptions of categories, you can use the filter category_description as follows:

    function quietness()
    {
    	return '';
    }
    add_filter('category_description', 'quietness');
    
    wp_list_categories();

    Setup use_desc_for_title=1 when you call wp_list_categories() to disable it.

    #8031
  16. thanks to you it was a fast fix!

    #8330

Leave a Reply

To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.