[wp-trac] [WordPress Trac] #20783: wp_unique_term_slug() have no filter

WordPress Trac wp-trac at lists.automattic.com
Wed May 30 02:24:05 UTC 2012


#20783: wp_unique_term_slug() have no filter
--------------------------+-----------------------------
 Reporter:  bolo1988      |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 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);
 }

 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/20783>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list