wp_unique_term_slug( string $slug, object $term ): string
- Since
- 2.3.0
- Source
wp-includes/taxonomy.php:3095
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
$slugwill 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
- Scaling
- Scales with input
- Instructions
- 26–114
- Plugin surface
- 2 hooks
- Called by
- 2
Reaches the database via ->get_var().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 125.
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.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - querycontent query
get_term()called directly - hookthird-party callbacks
apply_filters()called directly - sqldatabase query
->get_var()called directly - cacheobject cache
wp_cache_get()one call below wp_unique_term_slug() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
!term_exists() && !apply_filters() | 26 | term_exists(), apply_filters(), apply_filters() |
term_exists() && !apply_filters() | 30 | term_exists(), get_option(), apply_filters(), apply_filters() |
!term_exists() && !apply_filters() | 32–38 | term_exists(), is_taxonomy_hierarchical(), apply_filters(), apply_filters() |
term_exists() && !apply_filters() | 36–42 | term_exists(), get_option(), is_taxonomy_hierarchical(), apply_filters(), apply_filters() |
term_exists() && !apply_filters() | 38–39 | term_exists(), get_option(), get_term_by(), apply_filters(), apply_filters() |
!term_exists() && apply_filters() && !->get_var() | 42–47 | term_exists(), apply_filters(), ->prepare(), ->get_var(), apply_filters() |
term_exists() && !apply_filters() | 44–51 | term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), apply_filters(), apply_filters() |
term_exists() && apply_filters() && !->get_var() | 46–51 | term_exists(), get_option(), apply_filters(), ->prepare(), ->get_var(), apply_filters() |
!term_exists() && apply_filters() && !->get_var() | 48–59 | term_exists(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), apply_filters() |
!term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !apply_filters() | 49–51 | term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), apply_filters() |
term_exists() && apply_filters() && !->get_var() | 52–63 | term_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–55 | term_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–60 | term_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–61 | term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), term_exists(), apply_filters(), apply_filters() |
term_exists() && apply_filters() && !->get_var() | 60–72 | term_exists(), get_option(), get_term_by(), is_taxonomy_hierarchical(), apply_filters(), ->prepare(), ->get_var(), apply_filters() |
!term_exists() && apply_filters() && ->get_var() | 61–66 | term_exists(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters() |
term_exists() && is_taxonomy_hierarchical() && !empty($term) && !empty($the_parent) && !apply_filters() | 61–64 | term_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–65 | term_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–72 | term_exists(), is_taxonomy_hierarchical(), get_term(), is_wp_error(), apply_filters(), ->prepare(), ->get_var(), apply_filters() |
term_exists() && apply_filters() && ->get_var() | 65–70 | term_exists(), get_option(), apply_filters(), ->prepare(), ->get_var(), ->prepare(), ->get_var(), apply_filters() |
!term_exists() && apply_filters() && ->get_var() | 67–78 | term_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–76 | term_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–82 | term_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–74 | term_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–79 | term_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–82 | term_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–85 | term_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–86 | term_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–91 | term_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–91 | term_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–95 | term_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–95 | term_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–101 | term_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–104 | term_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–105 | term_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–114 | term_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:
- apply_filters( wp_unique_term_slug_is_bad_slug )filterline 3141 (+46 into the body)
Filters whether the proposed unique term slug is bad.
- apply_filters( wp_unique_term_slug )filterline 3172 (+77 into the body)
Filters the unique term slug.
Uses · 7
- term_exists()Determines whether a taxonomy term exists.
- get_option()Retrieves an option value based on an option name.
- get_term_by()Gets all term data from database by term field and data.
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- get_term()Gets all term data from database by term ID.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 2
- wp_insert_term()Adds a new term to the database.
- wp_update_term()Updates term based on arguments provided.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.