How to modify the default WordPress ‘Pages’ widget to have titles on the anchor tags?

So basically, the ‘Pages’ widget just prints out a list of all pages names as links (as you all probably know).

Although, these anchor tags do not have any titles, and that is frowned upon in the SEO world.

Read More

I would like to modify this to have the titles simply as the name of the page.

For example, I would like to turn this:

<ul>
    <li class="page_item page-item-3307"><a href="http://www.domain.com/about-us/">About Us</a></li>
    <li class="page_item page-item-3315"><a href="http://www.domain.com/blog/">Blog</a></li>
    <li class="page_item page-item-100"><a href="http://www.domain.com/contact/">Contact Us</a></li>
</ul>

Into this:

<ul>
    <li class="page_item page-item-3307"><a href="http://www.domain.com/about-us/" title="About Us">About Us</a></li>
    <li class="page_item page-item-3315"><a href="http://www.domain.com/blog/" title="Blog">Blog</a></li>
    <li class="page_item page-item-100"><a href="http://www.domain.com/contact/" title="Contact Us">Contact Us</a></li>
</ul>

I have had a look around within the wp-includes/default-widgets.php file, although it is all pushed through a function called wp_list_pages.

What must I do to achieve this?

PS. Using WordPress 3.8.1

Related posts

1 comment

  1. A fast and short solution would be –

    function wpse_list_pages( $output ){
        $output = preg_replace( '/<a(.*?)>(.*?)</a>/', '<a$1 title="$2">$2</a>', $output);
        return $output;
    }
    add_filter('wp_list_pages', 'wpse_list_pages');
    

    And if you want a proper way, you could extend Walker_Page (ref: post-template.php) class and set the Pages Widget walker to class with widget_pages_args hook

Comments are closed.