WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 8 months ago

#19135 closed enhancement

wp_get_archives() needs a hook/filter — at Initial Version

Reported by: LeviMorrison Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

I would expect that it would have one. It was proposed at one point: http://core.trac.wordpress.org/ticket/2329

Consider the case where I want to add rel="nofollow" to links in my archive:

function nofollow($string) {
    $dom = DOMDocument::loadXML($string);
    $list = $dom->getElementsByTagName('a');
    foreach($list as $a) {
        if (!$a->hasAttribute('rel')) {
            $a->setAttribute('rel', 'nofollow');
        }
    }
    return $dom->saveHTML();
}
ob_start();
wp_get_archives('type=monthly&limit=12');
echo nofollow(ob_get_clean());

That seems really ugly, especially since I already use the nofollow function in other places using apply_filter;

A more elegant way:

function wrap($string, $tag) {
    return "<$tag>$string</$tag>";
}
function nofollow($string) {
    $dom = DOMDocument::loadXML($string);
    $list = $dom->getElementsByTagName('a');
    foreach($list as $a) {
        if (!$a->hasAttribute('rel')) {
            $a->setAttribute('rel', 'nofollow');
        }
    }
    return $dom->saveHTML();
}
add_filter('wp_get_archives', 'wrap', 'ul');
add_filter('wp_get_archives', 'nofollow');
wp_get_archives('type=monthly&limit=12');

Change History (0)

Note: See TracTickets for help on using tickets.