Opened 20 months ago
Last modified 20 months ago
#19135 new enhancement
wp_get_archives() needs a hook/filter
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | 2nd-opinion |
| Cc: | MZAWeb |
Description (last modified by scribu)
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 (11)
comment:2
scribu
— 20 months ago
- Type changed from feature request to enhancement
A better solution would be to add an 'echo' parameter instead.
comment:3
LeviMorrison
— 20 months 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.
comment:4
LeviMorrison
— 20 months 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.
comment:5
scribu
— 20 months ago
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
A patch for the 'echo' parameter is welcome.
comment:6
follow-up:
↓ 8
LeviMorrison
— 20 months 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.
comment:7
follow-up:
↓ 9
MZAWeb
— 20 months ago
- Cc MZAWeb added
There are quite a bit of filters applied to HTML outputs.. like wp_list_pages, wp_dropdown_cats, etc
comment:8
in reply to:
↑ 6
scribu
— 20 months 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?
comment:9
in reply to:
↑ 7
scribu
— 20 months 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.
comment:10
LeviMorrison
— 20 months 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.
comment:11
scribu
— 20 months 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.