Opened 14 years ago
Closed 11 years ago
#20783 closed enhancement (fixed)
Add filter to wp_unique_term_slug()
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 4.3 | Priority: | normal |
| Severity: | normal | Version: | 3.3.2 |
| Component: | Taxonomy | Keywords: | has-patch needs-testing |
| Focuses: | Cc: |
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 (3)
Change History (14)
#1
@
14 years ago
- Component changed from General to Taxonomy
- Keywords has-patch added
- Type changed from defect (bug) to enhancement
- Version set to 3.3.2
#2
@
14 years ago
- Summary changed from wp_unique_term_slug() have no filter to Add filter to wp_unique_term_slug()
#3
follow-up:
↓ 4
@
14 years 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_slugon an earlier return of $slug - Adds
wp_unique_term_slug_is_bad_hierarchical_slugandwp_unique_term_slug_is_bad_flat_slugfilters as introduced in [16960] for #15726 to allow filtering of potentially bad hierarchical or flat slugs. - Adds
pre_wp_unique_term_slugfilter 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 ofwp_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.)
#4
in reply to:
↑ 3
@
14 years ago
- 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_slugon an earlier return of $slug- Adds
wp_unique_term_slug_is_bad_hierarchical_slugandwp_unique_term_slug_is_bad_flat_slugfilters as introduced in [16960] for #15726 to allow filtering of potentially bad hierarchical or flat slugs.- Adds
pre_wp_unique_term_slugfilter 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 ofwp_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.
#8
@
11 years ago
- Keywords dev-feedback removed
- Milestone changed from Awaiting Review to 4.3
I like the idea of adding filters here, but I'm going to propose something a bit different. See 20783.3.diff.
First, I've rearranged the logic so that we return only once. This allows us to have just a single apply_filters() call on the return value, which I think is nicer.
Second, I've removed the distinction between the different 'is_bad' filters. wp_unique_post_slug() has branching logic for attachment vs hierarchical vs non-hierarchical. wp_unique_term_slug() doesn't branch in such a clean way, so having two different filters doesn't make sense to me. The taxonomy will be available in the $term object passed to the filter, so the callback can make its own distinction if necessary.
Does this reordering make sense, @coffee2code and others?
#9
follow-up:
↓ 10
@
11 years ago
It does, although I'd probably prefer a more verbose variable name than $is_bad.
We have only two weeks left to make a decision on this ticket for 4.3.
Patch 20783.diff adds 'wp_unique_term_slug' filter. The
$original_slugarg was put last to be consistent with the proposed patch forwp_unique_post_slug()in #20480.