[wp-trac] [WordPress Trac] #43271: Issue with term duplicate check in wp_insert_term

WordPress Trac noreply at wordpress.org
Thu Aug 9 13:56:45 UTC 2018


#43271: Issue with term duplicate check in wp_insert_term
-----------------------------------------+------------------------------
 Reporter:  strategio                    |       Owner:  (none)
     Type:  enhancement                  |      Status:  new
 Priority:  normal                       |   Milestone:  Awaiting Review
Component:  Taxonomy                     |     Version:  4.1
 Severity:  normal                       |  Resolution:
 Keywords:  has-patch reporter-feedback  |     Focuses:
-----------------------------------------+------------------------------

Comment (by strategio):

 Thanks for the answer @boonebgorges!

 **A filter would be perfectly fine in my opinion** and I already gave it a
 try (successfully). I would just ask to replace the parameter `$term` with
 `$slug` in the filter. Indeed, we search duplicates with the same parent +
 taxonomy + slug (but the term name can still be duplicated).

 On WPML side, we already have some code to check if a taxonomy slug is
 unique in its language, so we will reuse it in the new filter (like below
 for example):

 {{{#!php
 /**
  * ??? filter parameters to be confirmed.
  *
  * @param stdClass $duplicate_term Duplicate term row from terms table, if
 found.
  * @param string   $slug           Slug being inserted.
  * @param string   $taxonomy       Taxonomy name.
  *
  * @return stdClass|null
  */
 public function wp_insert_term_duplicate_term_check( $duplicate_term,
 $slug, $taxonomy ) {
         if ( $duplicate_term ) {

                 $lang = $this->lang_term_slug_save( $taxonomy );

                 if ( ! WPML_Terms_Translations::term_slug_exists( $slug,
 $taxonomy, $lang ) ) {
                         $duplicate_term = null;
                 }
         }

         return $duplicate_term;
 }
 }}}


 To reply to your other questions:

 >By this, I assume you mean that you are using a filter on the return
 value of get_term_by() or wp_unique_term_slug()
 => We currently filter `pre_term_slug` but we'll change it to
 `wp_unique_term_slug` (I think our code existed before
 `wp_unique_term_slug` was available).

 >by "*can* live together" you mean "our plugin triggers duplicate slugs
 within a taxonomy and WP continues to work" - correct?
 => Exactly.

 >Or, alternatively, do you mean that it's possible, using wp_update_term()
 *alone* (with no filters), to update a term so that it shares a slug with
 a sibling from the same taxonomy?
 => No, this not possible "alone". It works for us because we have a filter
 on `pre_term_slug` (which will be changed to `wp_unique_term_slug`).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/43271#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list