WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 16 months ago

#20783 new enhancement

Add filter to wp_unique_term_slug()

Reported by: bolo1988 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.3.2
Component: Taxonomy Keywords: has-patch dev-feedback 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 (2)

20783.diff (1.2 KB) - added by coffee2code 3 years ago.
20783.2.diff (2.6 KB) - added by coffee2code 3 years ago.
Updated patch to better align with wp_unique_post_slug()

Download all attachments as: .zip

Change History (8)

@coffee2code3 years ago

comment:1 @coffee2code3 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

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.

comment:2 @coffee2code3 years ago

  • Summary changed from wp_unique_term_slug() have no filter to Add filter to wp_unique_term_slug()

@coffee2code3 years ago

Updated patch to better align with wp_unique_post_slug()

comment:3 follow-up: @coffee2code3 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_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.)

comment:4 in reply to: ↑ 3 @wpsmith3 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_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 @SergeyBiryukov2 years ago

#22291 was marked as a duplicate.

comment:6 @Chouby16 months ago

  • Cc frederic.demarle@… added
Note: See TracTickets for help on using tickets.