WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 8 months ago

Last modified 8 months ago

#19135 closed enhancement (wontfix)

wp_get_archives() needs a hook/filter

Reported by: 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)

comment:1 scribu2 years ago

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

comment:2 scribu2 years ago

  • Type changed from feature request to enhancement

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

comment:3 LeviMorrison2 years ago

That is simply this use case, one taken directly from my work. I can conceive of other types of filter. I do agree that a returning the output instead of echoing it would be an improvement.

Version 0, edited 2 years ago by LeviMorrison (next)

comment:4 LeviMorrison2 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.

comment:5 scribu2 years 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: LeviMorrison2 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 2 years ago by LeviMorrison (previous) (diff)

comment:7 follow-up: MZAWeb2 years 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 scribu2 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?

comment:9 in reply to: ↑ 7 scribu2 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.

comment:10 LeviMorrison2 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.

comment:11 scribu2 years ago

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

comment:12 c3mdigital8 months 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.

comment:13 SergeyBiryukov8 months ago

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