[wp-trac] [WordPress Trac] #29255: wp_insert_term() calls term_exists() with 'slug' where 'slug' gets compared to term 'name'

WordPress Trac noreply at wordpress.org
Mon Aug 18 22:49:55 UTC 2014


#29255: wp_insert_term() calls term_exists() with 'slug' where 'slug' gets compared
to term 'name'
--------------------------+-----------------------------
 Reporter:  Rob Walker    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Taxonomy      |    Version:  3.9.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 This may not specifically be a bug, but the resulting behavior is not what
 is expected.

 It appears that a new term slug gets checked against existing term names
 when wp_insert_term() calls term_exists($slug) on line 2430 of
 taxonomy.php.

 term_exists(), line 1645 of taxonomy.php, processes this slug as though it
 were the term name when checking against the db.

 The
 [http://codex.wordpress.org/Function_Reference/wp_insert_term#Return_Values
 documentation] reads:

 "If 'slug' argument exists then the slug will be checked to see if it is
 not a valid term. If that check succeeds (it is not a valid term), then it
 is added and the term id is given."

 Is that supposed to mean it checks slug against slug?

 Scenario:

 I have a taxonomy where the term names need to support duplicate strings
 therefore I am using the slugs as unique identifiers. To prevent
 collisions when inserting new terms I compare slugs and not names.

 Specifics (an example of how this issue came to light):

 I am working a taxonomy of student_names where the term name = the
 student's first name. The student's family name is handled in a separate
 taxonomy and is not appropriate to use as a unique identifier (the family
 name needs to be associated with the parents and possibly other students).

 When a new student is added, I generate the slug and ensure that it will
 be unique in the db.

 Normally the first student with a given name, say Craig, will be given the
 default slug "craig". I call wp_insert_term("Craig", "student_taxonomy",
 array($slug=>"craig")). It is the first term "Craig", wp_insert_term()
 finds no collision. Execution completes successfully.

 Adding an second student named Craig will result in my custom methods
 detecting the collision and generating a new slug "craig1". I call
 wp_insert_term("Craig", "student_taxomony", array($slug=>"craig1")).
 wp_insert_term() calls term_exists() with the slug "craig1", line 2430.
 term_exists checks "craig1" against existing '''term names''' (Craig),
 finds no conflict, execution completes successfully.

 A third Craig insertion generates the slug "craig2" which is compared
 against the existing '''term names''' (Craig, Craig), finds no conflict,
 execution completes successfully.

 Now, if the first Craig with the slug "craig" is deleted, and a fourth
 Craig is added, my code will generate the slug "craig" because there is no
 longer a conflict with that slug. Eventually wp_insert_term compares the
 slug "craig" to the '''term names''' "Craig, Craig" and finds a conflict.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29255>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list