Make WordPress Core

Opened 4 months ago

Last modified 5 days ago

#58404 new enhancement

Add pre_update_term filter hook

Reported by: kobi1's profile kobi1 Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch needs-unit-tests changes-requested 2nd-opinion reporter-feedback
Focuses: Cc:

Description

Hi
In file:

wp-includes/taxonomy.php

In function:

wp_update_term

How can to validate a custom term before updated?
A filter needs here like pre_insert_term in wp_insert_term() functin.
I think this filter has been forgotten in here.

$term = apply_filters( 'pre_edit_term', $term, $taxonomy, $args );

Attachments (3)

58404.patch (851 bytes) - added by nihar007 4 months ago.
I added a filter hook before update a term called pre_update_term
58404-2.patch (2.0 KB) - added by nihar007 3 months ago.
58404-3.patch (768 bytes) - added by nihar007 6 weeks ago.
I tested my last patch and saw, there was some unwanted code. I'm so sorry for this. I added the correct code and also wrote the right comment.

Download all attachments as: .zip

Change History (23)

@nihar007
4 months ago

I added a filter hook before update a term called pre_update_term

#1 @kobi1
4 months ago

  • Keywords has-patch added
  • Summary changed from pre_edit_term filter to Add pre_update_term filter hook

This ticket was mentioned in PR #4528 on WordPress/wordpress-develop by 2mkabir.


4 months ago
#2

Hi
In file:
wp-includes/taxonomy.php
In function:
wp_update_term
How can to validate a custom term before updated?
A filter needs here like pre_insert_term in wp_insert_term() functin.
I think this filter has been forgotten in here.
$term = apply_filters( 'pre_update_term', $term, $taxonomy, $args );

https://core.trac.wordpress.org/ticket/58404

Trac ticket:

#3 @davidbaumwald
4 months ago

  • Focuses administration removed
  • Milestone changed from Awaiting Review to 6.3
  • Severity changed from critical to normal
  • Version 6.2.2 deleted

#4 @oglekler
3 months ago

  • Keywords needs-testing needs-unit-tests added

I am adding needs-testing to highlight that this patch is ready for testing, and also needs-unit-tests as a suggestion that we need to check that this filter will be fired correctly in the future. It shuld be a simple enought Unit test.

#5 follow-up: @azaozz
3 months ago

Looking at 58404.patch, why is the filter before the if ( is_wp_error( $term ) )? I.e. should it be filtering erroneous updates for non-existing terms? Seems not?

This ticket was mentioned in Slack in #core by chaion07. View the logs.


3 months ago

#7 @mikeschroder
3 months ago

  • Keywords reporter-feedback added
  • Milestone changed from 6.3 to 6.4

Thanks everyone for your work on this!

This ticket was reviewed by a team of folks, including @oglekler and @chaion07 in a 6.3 ticket triage chat today, as enhancements need to be resolved or moved out of the 6.3 milestone before Beta 1, today.

As there's been recent feedback from @azaozz and @oglekler that hasn't yet been addressed/answered, I'm going to move this to the 6.4 milestone so that there is more time to work on the enhancement.

#8 in reply to: ↑ 5 @nihar007
3 months ago

Replying to azaozz:

Hi, I got the point. I added another patch.

@nihar007
3 months ago

This ticket was mentioned in Slack in #core by oglekler. View the logs.


6 weeks ago

#10 follow-up: @oglekler
6 weeks ago

  • Keywords changes-requested added; needs-testing reporter-feedback removed

Hi @nihar007, this ticket was discussed during a bug scrub. It looks like PR has unexpected changes. And also this new filter needs to be covered by Unit tests (you can look existing tests for reference). And since 6.4 should be since 6.4.0.

Add props to @mukesh27

@nihar007
6 weeks ago

I tested my last patch and saw, there was some unwanted code. I'm so sorry for this. I added the correct code and also wrote the right comment.

#11 in reply to: ↑ 10 @nihar007
6 weeks ago

Replying to oglekler:

Hi @oglekler,
I tested my last patch and saw, there was some unwanted code. I'm so sorry for this. I added the correct code and also wrote the right comment.

#12 @oglekler
2 weeks ago

Hi @nihar007, I assume this filter needs to be covered with Unit test to ensure that it will be present and working from now on. I suppose, it should be going into tests\phpunit\tests\term\wpUpdateTerm.php
and test_count_should_not_pass_through_main_get_terms_filter() function can be used as an example of a test or https://github.com/WordPress/wordpress-develop/pull/4717/files - this can be useful as well.

This ticket was mentioned in Slack in #core by oglekler. View the logs.


10 days ago

#14 @oglekler
10 days ago

This ticket was discussed during bug scrub.

@mukesh27 offered to take a closer look :)

This ticket was mentioned in Slack in #core by oglekler. View the logs.


5 days ago

joemcgill commented on PR #4528:


5 days ago
#16

Sorry for the mistaken review request. Looking at this, I'm unsure what the filter would provide that the current wp_update_term_data filter does not. Could you provide an example use case where this would be useful, @2mkabir?

#17 @joemcgill
5 days ago

  • Keywords 2nd-opinion added

#18 @joemcgill
5 days ago

  • Keywords reporter-feedback added
  • Milestone changed from 6.4 to Future Release

Moving to future release and marking for a second opinion regarding my question on the PR.

#19 @hellofromTonya
5 days ago

Sorry for the mistaken review request. Looking at this, I'm unsure what the filter would provide that the current wp_update_term_data filter does not. Could you provide an example use case where this would be useful?

I agree with @joemcgill in that I'm not sure what the filter is accomplishing and the use case for it.

In the latest patch 58404-3.patch, what happens if the filtered $term is no longer an object or instance of WP_Term? A safeguard check is needed to protect user sites.

@kobi1 commented on PR #4528:


5 days ago
#20

Hi @joemcgill. I need pre_update_term filter like this:

$term = apply_filters( 'pre_update_term', $term, $taxonomy, $args );

if ( is_wp_error( $term ) ) {
     return $term;
}

By utilizing this filter, I can supply a custom WP_Error() object as the return value for $term. This is analogous to the behavior of the pre_insert_term filter.

It serves its purpose within the /wp-admin/edit-tags.php page, specifically in the validation process of the custom field within the "quick edit" form.

### About my use case

I have created a plugin for Woocommerce select-quantity-by-attribute-for-woocommerce
I have added a custom field to the configure terms page of the product attributes. A sample url of this page:
/wp-admin/edit-tags.php?taxonomy=pa_quantity&post_type=product
This custom field is require and also added to the quick edit form.
My problem is that there is no way for validation my custom require field.

My custom field (Quantity). I want to show the error message if the Quantity field be empty
![](https://i.ibb.co/bLSkwVC/Screenshot-2023-09-26-014130.png)

Show error message for name field that is default of the Wordpress.
I want to do this work for the Quantity field.
![](https://i.ibb.co/DD8tpL5/Screenshot-2023-09-26-014342.png)

This is my code that doing validation with pre_insert_term filter. Also I want to do like this with pre_update_term filter.
https://github.com/2mkabir/select-quantity-by-attribute-for-woocommerce/blob/master/select-quantity-by-attribute-for-woocommerce.php#L99

Note: See TracTickets for help on using tickets.