wppaste
WordPress

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

Since
2.3.0
Source
wp-includes/taxonomy.php:3105
Makes term slug unique, if it isn't already.

Description

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 a unique slug, then it tries 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.

Compatibility

WordPress
since 2.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$slugstring
The string that will be tried for a unique slug.
$termobject
The term object that the $slug will belong to.

Return value

string
Will return a true unique slug.

Performance profile

How much work a call to wp_unique_term_slug() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_var().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
26–114

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 125.

Plugin surface
2 hooks

Third-party callbacks on 'wp_unique_term_slug_is_bad_slug', 'wp_unique_term_slug' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • querycontent queryget_term()called directly
  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->get_var()called directly
  • cacheobject cachewp_cache_get()one call below wp_unique_term_slug()
  • serializeserialisationmaybe_unserialize()one call below wp_unique_term_slug()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 36 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_unique_term_slug() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!term_exists() && !apply_filters()26term_exists(), apply_filters(), apply_filters()
term_exists() && !apply_filters()30term_exists(), get_option(), apply_filters(), apply_filters()
!term_exists() && !apply_filters()32–38term_exists(), is_taxonomy_hierarchical(), apply_filters(), apply_filters()
term_exists() && !apply_filters()36–42term_exists(), get_option(), is_taxonomy_hierarchical(), apply_filters(), apply_filters()
term_exists() && !apply_filters()38–39term_exists(), get_option(), get_term_by(), apply_filters(), apply_filters()
!term_exists() && apply_filters() && !->get_var()42–47term_exists(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && !apply_filters()44–51term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), apply_filters(), apply_filters()
term_exists() && apply_filters() && !->get_var()46–51term_exists(), get_option(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && apply_filters() && !->get_var()48–59term_exists(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !apply_filters()49–51term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), apply_filters()
term_exists() && apply_filters() && !->get_var()52–63term_exists(), get_option(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !apply_filters()53–55term_exists(), get_option(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), apply_filters()
24 further outcomes, up to 114 instructions
term_exists() && apply_filters() && !->get_var()54–60term_exists(), get_option(), get_term_by(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && !apply_filters()59–61term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), apply_filters()
term_exists() && apply_filters() && !->get_var()60–72term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && apply_filters() && ->get_var()61–66term_exists(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !apply_filters()61–64term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && !apply_filters()63–65term_exists(), get_option(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), apply_filters()
!term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && apply_filters() && !->get_var()65–72term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && apply_filters() && ->get_var()65–70term_exists(), get_option(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && apply_filters() && ->get_var()67–78term_exists(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && apply_filters() && !->get_var()69–76term_exists(), get_option(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && apply_filters() && ->get_var()71–82term_exists(), get_option(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && !apply_filters()71–74term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), apply_filters()
term_exists() && apply_filters() && ->get_var()73–79term_exists(), get_option(), get_term_by(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && apply_filters() && !->get_var()75–82term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && apply_filters() && !->get_var()77–85term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && apply_filters() && !->get_var()79–86term_exists(), get_option(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && apply_filters() && ->get_var()79–91term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && apply_filters() && ->get_var()84–91term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && apply_filters() && !->get_var()87–95term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && apply_filters() && ->get_var()88–95term_exists(), get_option(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
!term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && apply_filters() && ->get_var()94–101term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && apply_filters() && ->get_var()96–104term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && apply_filters() && ->get_var()98–105term_exists(), get_option(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !is_wp_error() && !empty($parent_term) && apply_filters() && ->get_var()106–114term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 125 instructions, 26–114 executed per call, 16 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_unique_term_slug() runs, in this order:

  1. apply_filters( wp_unique_term_slug_is_bad_slug )filterline 3151 (+46 into the body)

    Filters whether the proposed unique term slug is bad.

  2. apply_filters( wp_unique_term_slug )filterline 3182 (+77 into the body)

    Filters the unique term slug.

Uses · 7

Used by · 2

Source code

function wp_unique_term_slug( $slug, $term ) {	global $wpdb; 	$needs_suffix  = true;	$original_slug = $slug; 	// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.	if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {		$needs_suffix = false;	} 	/*	 * If the taxonomy supports hierarchy and the term has a parent, make the slug unique	 * by incorporating parent slugs.	 */	$parent_suffix = '';	if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {		$the_parent = $term->parent;		while ( ! empty( $the_parent ) ) {			$parent_term = get_term( $the_parent, $term->taxonomy );			if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {				break;			}			$parent_suffix .= '-' . $parent_term->slug;			if ( ! term_exists( $slug . $parent_suffix ) ) {				break;			} 			if ( empty( $parent_term->parent ) ) {				break;			}			$the_parent = $parent_term->parent;		}	} 	// If we didn't get a unique slug, try appending a number to make it unique. 	/**	 * Filters whether the proposed unique term slug is bad.	 *	 * @since 4.3.0	 *	 * @param bool   $needs_suffix Whether the slug needs to be made unique with a suffix.	 * @param string $slug         The slug.	 * @param object $term         Term object.	 */	if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {		if ( $parent_suffix ) {			$slug .= $parent_suffix;		} 		if ( ! empty( $term->term_id ) ) {			$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );		} else {			$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );		} 		if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared			$num = 2;			do {				$alt_slug = $slug . "-$num";				++$num;				$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );			} while ( $slug_check );			$slug = $alt_slug;		}	} 	/**	 * Filters the unique term slug.	 *	 * @since 4.3.0	 *	 * @param string $slug          Unique term slug.	 * @param object $term          Term object.	 * @param string $original_slug Slug originally passed to the function for testing.	 */	return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );}

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.