Custom Archives Widget with nofollow’d links

Applies to Version(s): Thesis 1

WordPress offers a default Archives Widget which lists your Monthly Archives, but there is no configuration option available in this widget to make these links “nofollow”. Here’s how you can make your own custom Archives Widget, which includes the nofollow attribute automatically.

There are three pieces of code to be added to the custom_functions.php file.

First, we’ll need a filter function which can be applied directly to the widget — since we may have other archive links which we do want followed by search engines, we need a way to limit the addition of the nofollow attribute to only those archive links which are generated by our custom widget:

// Filter to add nofollow attribute
function nofollow_archives($link_html) {
    return str_replace('<a href=', '<a rel="nofollow" href=',  $link_html); 
}

Next, we’ll need the code to create the widget itself, and this is where we’ll also employ the filter we created above (then remove it after we’re done with it, to avoid affecting other archive link listings on our pages):

// Create my archives widget
function my_archives_widget() { ?>
<li class="widget">
        <h3>My Archives</h3>
        <div class="widget widget_text"><ul>
        <?php add_filter('get_archives_link', 'nofollow_archives');  
        get_archives('postbypost', '3', 'html', '<li class="page_item">','</li>', FALSE);
        remove_filter('get_archives_link', 'nofollow_archives'); ?>
    </ul></div>
</li>
<?php
}
add_action('widgets_init', 'my_archives_widget_init');

Finally, we need to register the widget, so it’s available to us from Appearance > Widgets:

// Register my archives widget
function my_archives_widget_init() {
  register_sidebar_widget('My Archives Widget', 'my_archives_widget');
}

Now, we can simply drag and drop the My Archives Widget into the sidebar (or other widgetized area) of our choosing!