Make WordPress Core

Opened 12 months ago

Last modified 3 months ago

#60532 new enhancement

Add a `pre_set_object_terms` action

Reported by: davidbaumwald's profile davidbaumwald Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.5
Component: Taxonomy Keywords: good-first-bug has-patch
Focuses: Cc:

Description

Working with some custom terms & taxonomies, and it looks like there's no way to hook in to wp_set_object_terms _before_ the terms are updated.

In the end, the $old_tt_ids is passed to the set_object_terms action(added in r12261). But, these are the term_taxonomy_ids and a a bit harder to work with than the plain terms. Also, these are provided _after_ the terms are updated. So, there's no way to short-circuit the updating.

However, I think it would be useful to have a pre_ action that can work on the current terms before commiting the changes.

Attachments (1)

60532.patch (866 bytes) - added by sagarsavani7979 12 months ago.
This commit adds a new action hook, 'pre_set_object_terms', to the wp_set_object_terms function. This hook fires before an object's terms are set, allowing developers to perform operations or modifications on the terms array before the changes are committed.

Download all attachments as: .zip

Change History (7)

#1 @davidbaumwald
12 months ago

  • Keywords good-first-bug needs-patch added

@sagarsavani7979
12 months ago

This commit adds a new action hook, 'pre_set_object_terms', to the wp_set_object_terms function. This hook fires before an object's terms are set, allowing developers to perform operations or modifications on the terms array before the changes are committed.

#2 @sagarsavani7979
12 months ago

  • Keywords has-patch added; needs-patch removed

This ticket was mentioned in PR #7660 on WordPress/wordpress-develop by @geekofshire.


3 months ago
#3

This PR introduces a new pre_set_object_terms action that triggers before terms are assigned to an object in wp_set_object_terms. Currently, it is only possible to access term taxonomy IDs ($old_tt_ids) after terms have already been updated, which limits the ability to make adjustments prior to committing changes.

Trac ticket: https://core.trac.wordpress.org/ticket/60532

#4 @geekofshire
3 months ago

@sagarsavani7979 @davidbaumwald I have raised a PR, please do give it a look

#5 follow-up: @apermo
3 months ago

@geekofshire @sagarsavani7979 Imho the position of the filter is broken as it is in the patch and PR. The position is too low, $object_id is already used before to fill $old_tt_ids. So under normal conditions $old_tt_ids will have the wrong content if $append is false

Also see: https://github.com/WordPress/wordpress-develop/pull/7660#discussion_r1820132363

Last edited 3 months ago by apermo (previous) (diff)

#6 in reply to: ↑ 5 @geekofshire
3 months ago

Replying to apermo:

@geekofshire @sagarsavani7979 Imho the position of the filter is broken as it is in the patch and PR. The position is too low, $object_id is already used before to fill $old_tt_ids. So under normal conditions $old_tt_ids will have the wrong content if $append is false

Also see: https://github.com/WordPress/wordpress-develop/pull/7660#discussion_r1820132363

Thank you for the feedback! I’ve made adjustments based on your comments.

Note: See TracTickets for help on using tickets.