From 40e66f299a44cb1136d4b0529900ea2e74b7d400 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Sat, 28 Nov 2026 03:57:57 +0600 Subject: [PATCH] Taxonomy: Add a pre_update_term filter to wp_update_term(). 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. --- src/wp-includes/taxonomy.php | 24 ++++ tests/phpunit/tests/term/wpUpdateTerm.php | 155 ++++++++++++++++++++++ 2 files changed, 179 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 06d1d3581dd00..5ce4547362829 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -3419,6 +3419,30 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) { return new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); } + /** + * Filters a term before it is updated in the database. + * + * The filter only runs when the term exists. Returning a WP_Error object + * short-circuits the update and returns that error. Returning any other + * value that is not a WP_Term object short-circuits the update with an + * invalid_term error. + * + * @since 7.3.0 + * + * @param WP_Term $term Term object. + * @param string $taxonomy Taxonomy slug. + * @param array|string $args Array or query string of arguments passed to wp_update_term(). + */ + $term = apply_filters( 'pre_update_term', $term, $taxonomy, $args ); + + if ( is_wp_error( $term ) ) { + return $term; + } + + if ( ! $term instanceof WP_Term ) { + return new WP_Error( 'invalid_term', __( 'Invalid term.' ) ); + } + $term = (array) $term->data; // Escape data pulled from DB. diff --git a/tests/phpunit/tests/term/wpUpdateTerm.php b/tests/phpunit/tests/term/wpUpdateTerm.php index eff2dfbbe6852..353b05ae511c9 100644 --- a/tests/phpunit/tests/term/wpUpdateTerm.php +++ b/tests/phpunit/tests/term/wpUpdateTerm.php @@ -855,4 +855,159 @@ public function test_wp_update_term_child_with_long_encoded_slug_should_be_updat _unregister_taxonomy( $taxonomy ); } + + /** + * @ticket 58404 + */ + public function test_pre_update_term_filter_should_not_fire_when_term_does_not_exist() { + register_taxonomy( 'wptests_tax', 'post' ); + + $fired = false; + add_filter( + 'pre_update_term', + static function () use ( &$fired ) { + $fired = true; + return null; + } + ); + + $found = wp_update_term( 0, 'wptests_tax', array( 'name' => 'Updated' ) ); + + _unregister_taxonomy( 'wptests_tax' ); + + $this->assertWPError( $found ); + $this->assertSame( 'invalid_term', $found->get_error_code() ); + $this->assertFalse( $fired, 'pre_update_term fired for a term that does not exist.' ); + } + + /** + * @ticket 58404 + */ + public function test_pre_update_term_filter_should_receive_term_taxonomy_and_args() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + $captured = array(); + + add_filter( + 'pre_update_term', + static function ( $term, $taxonomy, $args ) use ( &$captured ) { + $captured = array( $term, $taxonomy, $args ); + return $term; + }, + 10, + 3 + ); + + $found = wp_update_term( $t, 'wptests_tax', array( 'description' => 'New description' ) ); + + _unregister_taxonomy( 'wptests_tax' ); + + $this->assertNotWPError( $found ); + $this->assertInstanceOf( WP_Term::class, $captured[0] ); + $this->assertSame( $t, $captured[0]->term_id ); + $this->assertSame( 'wptests_tax', $captured[1] ); + $this->assertSame( array( 'description' => 'New description' ), $captured[2] ); + } + + /** + * @ticket 58404 + */ + public function test_pre_update_term_filter_should_abort_update_with_wp_error() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'Original Name', + ) + ); + + add_filter( + 'pre_update_term', + static function () { + return new WP_Error( 'my_custom_error', 'My custom error.' ); + } + ); + + $edit_terms_fired = false; + add_action( + 'edit_terms', + static function () use ( &$edit_terms_fired ) { + $edit_terms_fired = true; + } + ); + + $found = wp_update_term( $t, 'wptests_tax', array( 'name' => 'Updated Name' ) ); + + $term = get_term( $t, 'wptests_tax' ); + + _unregister_taxonomy( 'wptests_tax' ); + + $this->assertWPError( $found ); + $this->assertSame( 'my_custom_error', $found->get_error_code() ); + $this->assertFalse( $edit_terms_fired, 'edit_terms fired after a pre_update_term short-circuit.' ); + $this->assertSame( 'Original Name', $term->name, 'The term was updated despite the pre_update_term short-circuit.' ); + } + + /** + * @ticket 58404 + */ + public function test_pre_update_term_filter_should_return_error_for_non_term_value() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'Original Name', + ) + ); + + add_filter( + 'pre_update_term', + static function () { + return 'not a term'; + } + ); + + $found = wp_update_term( $t, 'wptests_tax', array( 'name' => 'Updated Name' ) ); + + $term = get_term( $t, 'wptests_tax' ); + + _unregister_taxonomy( 'wptests_tax' ); + + $this->assertWPError( $found ); + $this->assertSame( 'invalid_term', $found->get_error_code() ); + $this->assertSame( 'Original Name', $term->name, 'The term was updated despite an invalid pre_update_term return value.' ); + } + + /** + * @ticket 58404 + */ + public function test_pre_update_term_filter_should_allow_update_to_proceed() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'Original Name', + ) + ); + + add_filter( + 'pre_update_term', + static function ( $term ) { + return $term; + } + ); + + $found = wp_update_term( $t, 'wptests_tax', array( 'name' => 'Updated Name' ) ); + + $term = get_term( $t, 'wptests_tax' ); + + _unregister_taxonomy( 'wptests_tax' ); + + $this->assertNotWPError( $found ); + $this->assertSame( 'Updated Name', $term->name ); + } }