Opened 4 years ago

Last modified 3 years ago

#9227 new enhancement

in get_the_category_list(), filter categories before constructing list

Reported by: KevinB Owned by: anonymous
Priority: normal Milestone: Future Release
Component: Template Version: 2.8
Severity: normal Keywords: category filter has-patch tested
Cc:

Description

The template function get_the_category_list returns a concatenated string of category links for an individual post. The only filtering available is on the full concatenated sting, via 'the_category' hook.

Since filtering the category array directly is greatly preferable for most uses, please consider adding the attached patch, or something like it.

Whatever hook is added should somehow indicate the data context - in this case for display. In filtering post categories, there is the potential for non-display situations in which the actual post categories must be maintained even though the display filtering hides some from the current user. What we should not do is apply a single filter inside get_the_category or wp_get_object_terms, since themes and plugins may already call those functions for multiple data contexts.

I had considered calling the new hook 'get_the_category_for_list'. Instead I'm suggesting a more generic 'get_the_category' hook, passing context as the second argument. That would leave the API consistent for possible future use in filtering other contexts.

Attachments (1)

get_the_category_display.patch (577 bytes) - added by KevinB 4 years ago.
direct filtering of the post categories array in get_the_category list

Download all attachments as: .zip

Change History (13)

KevinB4 years ago

direct filtering of the post categories array in get_the_category list

  • Keywords changed from category template filter to category, template, filter

comment:2 in reply to: ↑ description ; follow-up: ↓ 3   filosofo4 years ago

Replying to KevinB:

I had considered calling the new hook 'get_the_category_for_list'. Instead I'm suggesting a more generic 'get_the_category' hook, passing context as the second argument. That would leave the API consistent for possible future use in filtering other contexts.

So wouldn't it make more sense to add the filter to get_the_category()'s return?

comment:3 in reply to: ↑ 2   KevinB4 years ago

Replying to filosofo:

Replying to KevinB:

I had considered calling the new hook 'get_the_category_for_list'. Instead I'm suggesting a more generic 'get_the_category' hook, passing context as the second argument. That would leave the API consistent for possible future use in filtering other contexts.

So wouldn't it make more sense to add the filter to get_the_category()'s return?

The problem I see is that get_the_category() does not know the context it's called in, whereas get_the_category_list() would always be a display context. Since get_the_category() returns an array, some themes or plugins would not be unreasonable in using it in a raw/db context - for a maintenance operation which pertains to all stored post categories regardless of the currently logged user. Is this a valid concern, or would you just assume that get_the_category() is always for display purposes and all backend operations use wp_get_object_terms()?

I want to filter post categories in a display context for the current user (who may be restricted by my plugin), so I need a hook which will prevent me from fibbing to someone about the actual post categories.

  • Keywords has-patch added
  • Milestone changed from Unassigned to 2.8

+1. would naming the hook get_the_category_list instead make more sense?

  • Keywords tested commit added

see also #8704 and #9719

#8704 got fixed, we can probably close this one as fixed too.

  • Version changed from 2.7 to 2.8

The wp_get_object_terms filter added in #8704 is not, as the seasoned professionals say, a "robust" solution to the issue I raised here.

The wp_get_object_terms filter alone (and the term_cache which it loads) does not allow for any distinction between terms which an object has and terms which the current user can view.

  • Milestone changed from 2.8 to 2.9

punting per IRC discussion

patch still applies clean

  • Milestone changed from 2.9 to Future Release
  • Keywords template commit removed
Note: See TracTickets for help on using tickets.