Mar
11
2009

Remove the Title Attribute from WordPress Category and Page Lists

by   |  Posted in Tutorials  |  50 comments

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 instead of .

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

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , ,

Short URL: http://bit.ly/a3jbmJ

Discussion 50 Comments

  1. Tim on April 7, 2009 at 12:00 am

    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

  2. Tim on April 7, 2009 at 12:03 am

    Never mind… worked it out.

    Was missing the end }.

    Thanks again. Works perfectly.

    Tim

  3. Michael Kay on April 18, 2009 at 1:04 pm

    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:

  4. Michael Kay on April 18, 2009 at 1:06 pm

    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.

  5. Web Overhauls on June 24, 2009 at 4:13 pm

    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.)

  6. Jeff Federman on July 26, 2009 at 9:08 pm

    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;
    }

  7. Kenneth Watt on August 3, 2009 at 1:33 pm

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

    • c.bavota on August 5, 2009 at 11:13 am

      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.

    • b.parks on September 3, 2009 at 4:36 pm

      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).

    • Donny on October 31, 2009 at 9:17 pm

      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.

  8. Karar.A. on October 2, 2009 at 11:32 am

    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 :)

  9. Rich on November 3, 2009 at 8:34 pm

    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!

  10. Djor on November 22, 2009 at 10:25 pm

    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')); 
    ?>
  11. Tim Holt on January 4, 2010 at 12:01 pm

    Those who aren’t comfortable editing their theme files can use the Remove Title Attributes plugin to solve this problem.

    • Dean on June 22, 2010 at 3:59 am

      Thanks for this plugin Tim. Exaclty what I needed at this time of day.

    • paulo on August 4, 2010 at 6:43 pm

      Tim, thanks for your excellent plug-in. You have saved me from so much whining from my rather awkward client :)

      Much kudos and karma to you!

  12. Jeff on January 6, 2010 at 1:00 am

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

  13. Guy on February 2, 2010 at 4:19 pm

    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

    • c.bavota on February 3, 2010 at 11:41 am

      I wish everything was that easy. But alas, this one needs some hacking.

  14. Steve on February 2, 2010 at 4:26 pm

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

  15. piouPiouM on March 3, 2010 at 1:16 pm

    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.

  16. banesto on March 17, 2010 at 6:40 am

    thanks to you it was a fast fix!

  17. Jim Camomile on April 17, 2010 at 1:54 pm

    Hi,

    Maybe this is new in WP but I found a parameter for wp_list_cats that works nicely.

    use_desc_for_title = 0

    as in

    http://codex.wordpress.org/Function_Reference/wp_list_cats

  18. Bart on April 19, 2010 at 12:30 pm

    hello, is there a place where I can see how this tutorial is done? I am very new in all this and if I only could understand paste the codes this would really help.
    So in “functions.php”, which code of the two is best, the first was to be pasted where?
    And finally to call the category list what does this really mean?

    Sorry for asking! :-)

    • c.bavota on April 20, 2010 at 12:55 pm

      The functions.php code above isn’t necessary. That is only worth using if you plan on calling the code in many different file. Just paste the first block of code into the file where you want the list to appear.

    • Bart on April 20, 2010 at 4:06 pm

      Yes I still don’t get it, ups sorry! Where? Paste into the header.php… because there is the menu and there I have the alt tags appearing… ?

      And as Nathalie is commenting I also use qtranslate..will it work as well?

      Thanks again for your time.

    • c.bavota on April 21, 2010 at 9:44 am

      Use the code where you want your menu/list to appear. So if it is in the header.php file then replace the current code.

    • Bart on April 21, 2010 at 3:54 pm

      Dear friend, I paste the code of the header here. I tried to do it by guessing where it could be pasted your code, but no way it does not work, can you please tell me which line must be replaced with your code?

      <html xmlns="http://www.w3.org/1999/xhtml&quot; >

      <meta http-equiv="Content-Type" content="; charset=” />

      <link rel="stylesheet" href="” type=”text/css” media=”screen” />
      <link rel="alternate" type="application/rss+xml" title="RSS Feed" href="” />
      <link rel="pingback" href="” />

      <body id="” >

      <a href="”><img src="” alt=”" width=”" height=”"/>

      <h1 style="left:px;line-height:px”>

      <li><a href="” title=”">

      cat_ID; else : $current_cat = ”; endif;
      $ex_cats = ts_get_option(‘ts_ex_cats’); wp_list_categories(‘title_li=&current_category=’.$current_cat.’&exclude=’.$ex_cats.’&orderby=ID’); ?>

    • c.bavota on April 22, 2010 at 10:09 am

      Replace this:

      wp_list_categories(‘title_li=&current_category=’.$current_cat.’&exclude=’.$ex_cats.’&orderby=ID’);
  19. Nathalie on April 20, 2010 at 3:12 am

    I’m new to this as well. I use the Thesis theme and Qtranslate. I really want to get rid of the title attributes, because they look like this: [en:]english page title [nl:]dutch page title. Can someone explain us (Bart en me) in which file we have to paste the codes and which codes? Both?
    Thank you in advance.

    • c.bavota on April 20, 2010 at 12:53 pm

      You can just paste in the code wherever you want it to appear. You don’t have to put it into the function.php file.

  20. Nathalie on April 20, 2010 at 3:13 am

    By the way, I tried all the plugins made for this, but they don’t work.

  21. Nathalie on April 21, 2010 at 4:10 am

    Thanks for the advice. I tried everything. I have a website with static pages and one page with a blog. I tried to copy the codes into theme/functions.php and in theme/custom/custom_functions.php. Nothing works. Can you please be more specific to us newbies? Which files, which line do have to copy the codes, which codes exactly from beginning to end? I also tried Tim’s code with the end hook, but no result either.

    This is the only blog where codes are mentioned to delete the title attributes. Everyone else suggest the plugins, but they conflict with qtranslate.

    Thanks again.

    • c.bavota on April 21, 2010 at 9:42 am

      What theme are you using? It might not be using the functions.php file the same way that other themes do. For the most part, if you just copy the second code into the functions.php file anywhere, you can then use the function to call it in any file. Most likely, you would put it into the header.php file in the navigation div or wherever you want your menu to appear.

  22. Nathalie on April 22, 2010 at 3:58 am

    Thesis 1.7 is the theme.

    • c.bavota on April 22, 2010 at 10:09 am

      Thesis doesn’t really allow for easy addition of content. You would have to remove the current menu and then add the code above in. Look for instructions on how to do that on the Thesis Web site.

  23. Wilson on April 24, 2010 at 8:39 pm

    Not sure if this was mentioned, but you can add the title parameter with an empty string and that’ll remove them:

     
  24. Heinrich on June 23, 2010 at 1:37 am

    Very very helpful, I had no idea in how to change the title attribute, i was thinking to use jQuery, but this is better :) thanks!!

  25. tannar on August 11, 2010 at 7:23 am

    Thanks, this is very nice!