WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 10 months ago

#34055 new enhancement

the_taxonomies lacks a filter

Reported by: Chouby Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.5
Component: Taxonomy Keywords: has-patch
Focuses: template Cc:

Description

The template tag "the_taxonomies" displays all taxonomies terms attached to a post. However plugins can use taxonomies for internal purposes and not every taxonomy terms are meant to be displayed.

Attachments (2)

34055.patch (653 bytes) - added by Chouby 5 years ago.
34055.2.patch (1.4 KB) - added by Chouby 4 years ago.

Download all attachments as: .zip

Change History (7)

@Chouby
5 years ago

#1 @Chouby
5 years ago

  • Keywords has-patch added

The proposed patch filters the list of terms in 'get_the_taxonomies'. Although I would not need it myself, I suppose it could make sense to pass the post as second argument.

#2 follow-up: @DrewAPicture
5 years ago

  • Keywords reporter-feedback added

@Chouby: Are you wanting to filter the actual terms or the taxonomies?

#3 in reply to: ↑ 2 @Chouby
5 years ago

My goal would be to remove all the terms from non public taxonomies.

#4 follow-up: @boonebgorges
4 years ago

  • Keywords needs-patch needs-unit-tests added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release

A filter somewhere in get_the_taxonomies() makes sense. However, the filter suggested in 34055.patch doesn't seem right to me. It appears within a foreach ( $taxonomies ) loop, but it doesn't accept the taxonomy as a parameter. The filter name itself is a bit odd.

I realize you probably want to filter the data before the markup is generated. I guess my suggestion would be to break up the foreach() loop into two separate loops, with a filter between. Something like:

foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
    // ...
    $taxonomies[ $taxonomy ] = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
}

$taxonomies = apply_filters( 'get_the_taxonomies', $taxonomies, $post );

$links = array();
foreach ( $taxonomies as $taxonomy => $terms ) {
    foreach ( $terms as $term ) {
        $links[] = wp_sprintf( ... );
    }
}

// and maybe another filter on the output for good measure
return apply_filters( 'get_the_taxonomies_links', $links, $post, $args );

This seems much more appropriate for general use, and the filter names seem clearer to me. Chouby, will it serve your purposes?

If we go this way, we'll need unit tests verifying that there are no regressions from the refactoring of get_the_taxonomies(). (There are already some tests, but it's possible that more would be needed.)

@Chouby
4 years ago

#5 in reply to: ↑ 4 @Chouby
4 years ago

  • Keywords has-patch added; needs-patch needs-unit-tests reporter-feedback removed

Replying to boonebgorges:

It appears within a foreach ( $taxonomies ) loop, but it doesn't accept the taxonomy as a parameter. The filter name itself is a bit odd.

Putting the filter inside the loop was indeed not very clever.

I realize you probably want to filter the data before the markup is generated.

Yes. But my intention was not to filter the list of terms but the list of taxonomies. The goal is to remove non-public taxonomies, not individual terms. Thus, in 34055.2.patch, I put the filter before the loop. I also included the second filter that you proposed.

if you believe that it is preferrable to filter everything then I guess that your proposal is the way to go.

Note: See TracTickets for help on using tickets.