#19135 closed enhancement (wontfix)
wp_get_archives() needs a hook/filter
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | |
| Component: | General | Keywords: | |
| Focuses: | Cc: |
Description (last modified by )
I would expect that it would have one. It was proposed at one point: #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 (13)
#2
@
14 years ago
- Type changed from feature request to enhancement
A better solution would be to add an 'echo' parameter instead.
#3
@
14 years ago
That is simply this use case, one taken directly from my work. I can conceive of other types of filtering. I do agree that a returning the output instead of echoing it would be an improvement.
#4
@
14 years ago
Also, I know I could apply the filter directly to the links by hooking into that filter, but DOM operations are a bit more expensive to start up. Being able to do them all at once is better than one at a time. It's also important to note that a parser or DOMDocument is the correct way to do this, not using regex.
#5
@
14 years ago
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
A patch for the 'echo' parameter is welcome.
#6
follow-up:
↓ 8
@
14 years ago
It has an echo parameter already: http://codex.wordpress.org/Function_Reference/wp_get_archives.
However, I fail to see how adding a filter on raw HTML output is any different than filters in place for links, such as get_archives_link. You still modify raw HTML on those filters.
It is no different, in my opinion.
#7
follow-up:
↓ 9
@
14 years ago
- Cc MZAWeb added
There are quite a bit of filters applied to HTML outputs.. like wp_list_pages, wp_dropdown_cats, etc
#8
in reply to:
↑ 6
@
14 years ago
Replying to LeviMorrison:
It has an echo parameter already: http://codex.wordpress.org/Function_Reference/wp_get_archives.
Ok, so why do you need a filter, then?
#9
in reply to:
↑ 7
@
14 years ago
Replying to MZAWeb:
There are quite a bit of filters applied to HTML outputs.. like wp_list_pages, wp_dropdown_cats, etc
That doesn't mean they're a good idea.
#10
@
14 years ago
One reason is simply that filters are the 'wordpress' way of doing things. I have this function that I use in several places, just add_filter and I'm good to go. Having to use multiple methods of deploying the same function seems inconsistent and wrong.
#11
@
14 years ago
- Description modified (diff)
- Keywords 2nd-opinion added; needs-patch removed
- Milestone changed from Future Release to Awaiting Review
Adding a filter on raw html output, just to avoid output buffering, is not worth it, IMO.