wp_unique_term_slug

Will make slug unique, if it isn't already.

Description

(string) wp_unique_term_slug( (string) $slug, (object) $term ); 

The $slug has to be unique global to every taxonomy, meaning that one taxonomy term can't have a matching slug with another taxonomy term. Each slug has to be globally unique for every taxonomy.

The way this works is that if the taxonomy that the term belongs to is hierarchical and has a parent, it will append that parent to the $slug.

If that still doesn't return an unique slug, then it try to append a number until it finds a number that is truly unique.

The only purpose for $term is for appending a parent, if one exists.

Returns (string)

Will return a true unique slug.

Parameters (2)

0. $slug (string)
The string that will be tried for a unique slug.
1. $term (object)
The term object that the $slug will belong to.

Usage

  1. if ( !function_exists( 'wp_unique_term_slug' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The string that will be tried for a unique slug. 
  5. $slug = ''; 
  6.  
  7. // The term object that the `$slug` will belong to. 
  8. $term = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_unique_term_slug($slug, $term); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function wp_unique_term_slug( $slug, $term ) { 
  2. global $wpdb; 
  3.  
  4. $needs_suffix = true; 
  5. $original_slug = $slug; 
  6.  
  7. // As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies. 
  8. if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) { 
  9. $needs_suffix = false; 
  10.  
  11. /** 
  12. * If the taxonomy supports hierarchy and the term has a parent, make the slug unique 
  13. * by incorporating parent slugs. 
  14. */ 
  15. $parent_suffix = ''; 
  16. if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) { 
  17. $the_parent = $term->parent; 
  18. while ( ! empty($the_parent) ) { 
  19. $parent_term = get_term($the_parent, $term->taxonomy); 
  20. if ( is_wp_error($parent_term) || empty($parent_term) ) 
  21. break; 
  22. $parent_suffix .= '-' . $parent_term->slug; 
  23. if ( ! term_exists( $slug . $parent_suffix ) ) { 
  24. break; 
  25.  
  26. if ( empty($parent_term->parent) ) 
  27. break; 
  28. $the_parent = $parent_term->parent; 
  29.  
  30. // If we didn't get a unique slug, try appending a number to make it unique. 
  31.  
  32. /** 
  33. * Filters whether the proposed unique term slug is bad. 
  34. * @since 4.3.0 
  35. * @param bool $needs_suffix Whether the slug needs to be made unique with a suffix. 
  36. * @param string $slug The slug. 
  37. * @param object $term Term object. 
  38. */ 
  39. if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) { 
  40. if ( $parent_suffix ) { 
  41. $slug .= $parent_suffix; 
  42. } else { 
  43. if ( ! empty( $term->term_id ) ) 
  44. $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); 
  45. else 
  46. $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); 
  47.  
  48. if ( $wpdb->get_var( $query ) ) { 
  49. $num = 2; 
  50. do { 
  51. $alt_slug = $slug . "-$num"; 
  52. $num++; 
  53. $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); 
  54. } while ( $slug_check ); 
  55. $slug = $alt_slug; 
  56.  
  57. /** 
  58. * Filters the unique term slug. 
  59. * @since 4.3.0 
  60. * @param string $slug Unique term slug. 
  61. * @param object $term Term object. 
  62. * @param string $original_slug Slug originally passed to the function for testing. 
  63. */ 
  64. return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );