Repository navigation
Taxonomy: Add a pre_update_term filter to wp_update_term() - #16299
faisalahammad wants to merge 1 commit into
Conversation
Mirrors pre_insert_term in wp_insert_term(). Returning a WP_Error object short-circuits the update so callbacks can validate a term and reject the change before anything is written. Returning a value that is not a WP_Term object short-circuits the update with an invalid_term error to guard the code that follows against an unexpected filter return value. The filter only fires when the term exists, so missing terms keep returning their own errors untouched. Props kobi1, nihar007, 2mkabir, oglekler, azaozz, joemcgill, hellofromTonya, ankitp47. Fixes #58404.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Adds a
pre_update_termfilter towp_update_term(), modeled onpre_insert_terminwp_insert_term(), so a callback can reject a term update with aWP_Errorbefore anything is written.Trac ticket: https://core.trac.wordpress.org/ticket/58404
Description
The existing
wp_update_term_datafilter can change the data used for the update but cannot abort it, and returning aWP_Errorfrom it is ignored. This patch adds apre_update_termfilter as a gate, mirroring howpre_insert_termworks inwp_insert_term().Details, addressing the discussion on the ticket:
WP_Error(feedback from azaozz in comment 5).WP_Errorfrom the filter short-circuits the update and is returned as-is. This is the use case from the ticket and closes the gap confirmed in comment 21.WP_Termobject short-circuits the update with aninvalid_termerror, so an unexpected return value cannot reach the code that follows. This is the safeguard discussed in comments 19 and 21.wp_update_term_datafilter. The new filter is only a gate.One observation for discussion: the
alias_ofhandling in bothwp_insert_term()andwp_update_term()callswp_update_term()internally to keep the term group in sync and discards the return value, so a veto from the filter during that internal call would be silent. That is a pre-existing pattern that also applies towp_update_term_data, and handling it would change the error contracts of both functions, so it is left out of this patch.This supersedes #4528 and includes the feedback raised on the ticket there.
Testing Instructions
Run the new unit tests:
To test manually, add the following and then edit a term in wp-admin:
Updates to a term with the slug
lockedare rejected with the error, andedit_termsnever fires for it. Other terms update normally.Use of AI Tools
AI assistance: Yes
Tool(s): ZCode
Model(s): GLM 5.3
Used for: Ticket research, the patch and unit tests were drafted with AI assistance; the implementation, tests, and coding standards were reviewed and verified by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.