WordPress.org

Make WordPress Core

Ticket #21093: wp_unique_term_slug.patch

File wp_unique_term_slug.patch, 1.7 KB (added by wpsmith, 6 years ago)

First pass

  • wordpress/wp-includes/taxonomy.php

     
    22412241function wp_unique_term_slug($slug, $term) {
    22422242        global $wpdb;
    22432243
    2244         if ( ! term_exists( $slug ) )
    2245                 return $slug;
     2244        if ( ! term_exists( $slug ) && ! apply_filters( 'wp_unique_is_bad_term_slug', false, $slug, $term ) )
     2245                return apply_filters( 'wp_unique_term_slug', $slug, $term );
    22462246
    22472247        // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
    22482248        // by incorporating parent slugs.
    2249         if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
     2249        if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) || apply_filters( 'wp_unique_is_bad_term_slug', false, $slug, $term ) ) {
    22502250                $the_parent = $term->parent;
    22512251                while ( ! empty($the_parent) ) {
    22522252                        $parent_term = get_term($the_parent, $term->taxonomy);
     
    22542254                                break;
    22552255                        $slug .= '-' . $parent_term->slug;
    22562256                        if ( ! term_exists( $slug ) )
    2257                                 return $slug;
     2257                                return apply_filters( 'wp_unique_term_slug', $slug, $term );
    22582258
    22592259                        if ( empty($parent_term->parent) )
    22602260                                break;
     
    22682268        else
    22692269                $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
    22702270
    2271         if ( $wpdb->get_var( $query ) ) {
     2271        if ( $wpdb->get_var( $query ) || apply_filters( 'wp_unique_is_bad_term_slug', false, $slug, $term ) ) {
    22722272                $num = 2;
    22732273                do {
    22742274                        $alt_slug = $slug . "-$num";
     
    22782278                $slug = $alt_slug;
    22792279        }
    22802280
    2281         return $slug;
     2281        return apply_filters( 'wp_unique_term_slug', $slug, $term );
    22822282}
    22832283
    22842284/**