Opened 12 months ago
Last modified 7 months ago
#20783 new enhancement
Add filter to wp_unique_term_slug()
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Taxonomy | Version: | 3.3.2 |
| Severity: | normal | Keywords: | has-patch dev-feedback needs-testing |
| Cc: | travis@… |
Description
I can't modify the result of wp_unique_term_slug(), but if using the following code:
function wp_unique_term_slug($slug, $term) {
global $wpdb;
if ( ! term_exists( $slug ) )
return $slug;
// If the taxonomy supports hierarchy and the term has a parent, make the slug unique
// by incorporating parent slugs.
if ( 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;
$slug .= '-' . $parent_term->slug;
if ( ! term_exists( $slug ) )
return $slug;
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.
if ( !empty($args['term_id']) )
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $args['term_id'] );
else
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
if ( $wpdb->get_var( $query ) ) {
$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;
}
return apply_filters('unique_term_slug', $slug);
}
Attachments (2)
Change History (7)
coffee2code — 11 months ago
comment:1
coffee2code — 11 months ago
- Component changed from General to Taxonomy
- Keywords has-patch added
- Type changed from defect (bug) to enhancement
- Version set to 3.3.2
comment:2
coffee2code — 11 months ago
- Summary changed from wp_unique_term_slug() have no filter to Add filter to wp_unique_term_slug()
comment:3
follow-up:
↓ 4
coffee2code — 11 months ago
Patch 20783.2.diff brings wp_unique_term_slug() into greater alignment with its post counterpart, wp_unique_post_slug():
- Calls wp_unique_term_slug on an earlier return of $slug
- Adds wp_unique_term_slug_is_bad_hierarchical_slug and wp_unique_term_slug_is_bad_flat_slug filters as introduced in [16960] for #15726 to allow filtering of potentially bad hierarchical or flat slugs.
- Adds pre_wp_unique_term_slug filter early in function to short-circuit add the db queries, filters, etc if the hooking function's intent is to fully implement its own slug handling (the only thing not already part of wp_unique_post_slug(), but proposed in #21112)
(The addition of the *is_bad* slugs prompted by #21093, which was closed in favor of this ticket.)
- Cc travis@… added
- Keywords dev-feedback needs-testing added
Replying to coffee2code:
Patch 20783.2.diff brings wp_unique_term_slug() into greater alignment with its post counterpart, wp_unique_post_slug():
- Calls wp_unique_term_slug on an earlier return of $slug
- Adds wp_unique_term_slug_is_bad_hierarchical_slug and wp_unique_term_slug_is_bad_flat_slug filters as introduced in [16960] for #15726 to allow filtering of potentially bad hierarchical or flat slugs.
- Adds pre_wp_unique_term_slug filter early in function to short-circuit add the db queries, filters, etc if the hooking function's intent is to fully implement its own slug handling (the only thing not already part of wp_unique_post_slug(), but proposed in #21112)
(The addition of the *is_bad* slugs prompted by #21093, which was closed in favor of this ticket.)
Thanks coffee2code for incorporating my patch with this patch.
comment:5
SergeyBiryukov — 7 months ago
#22291 was marked as a duplicate.

Patch 20783.diff adds 'wp_unique_term_slug' filter. The $original_slug arg was put last to be consistent with the proposed patch for wp_unique_post_slug() in #20480.