Make WordPress Core

Opened 13 years ago

Closed 11 years ago

Last modified 11 years ago

#19135 closed enhancement (wontfix)

wp_get_archives() needs a hook/filter

Reported by: levimorrison's profile LeviMorrison Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

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 (13)

#1 @scribu
13 years ago

Adding a filter on raw html output, just to avoid output buffering, is not worth it, IMO.

#2 @scribu
13 years ago

  • Type changed from feature request to enhancement

A better solution would be to add an 'echo' parameter instead.

#3 @LeviMorrison
13 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.

Last edited 13 years ago by LeviMorrison (previous) (diff)

#4 @LeviMorrison
13 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 @scribu
13 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: @LeviMorrison
13 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.

Last edited 13 years ago by LeviMorrison (previous) (diff)

#7 follow-up: @MZAWeb
13 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 @scribu
13 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 @scribu
13 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 @LeviMorrison
13 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 @scribu
13 years ago

  • Description modified (diff)
  • Keywords 2nd-opinion added; needs-patch removed
  • Milestone changed from Future Release to Awaiting Review

#12 @c3mdigital
11 years ago

  • Keywords 2nd-opinion removed
  • Resolution set to wontfix
  • Status changed from new to closed

A filter just so raw html can be parsed doesn't seem like a real need.

#13 @SergeyBiryukov
11 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.