[wp-trac] [WordPress Trac] #19135: wp_get_archives() needs a hook/filter
WordPress Trac
wp-trac at lists.automattic.com
Thu Nov 3 22:12:20 UTC 2011
#19135: wp_get_archives() needs a hook/filter
-----------------------------+-----------------------------
Reporter: LeviMorrison | Owner:
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Keywords:
-----------------------------+-----------------------------
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');
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/19135>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list