diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index c7a923e8da..92ec413728 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -2903,15 +2903,29 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { } } - // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy. - if ( $existing_term = get_term_by( 'name', $name, $taxonomy ) ) { - if ( is_taxonomy_hierarchical( $taxonomy ) ) { - $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) ); - if ( in_array( $name, $siblings ) ) { - return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id ); + /* + * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy, + * unless a unique slug has been explicitly provided. + */ + if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) { + $slug_match = get_term_by( 'slug', $slug, $taxonomy ); + if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) { + if ( is_taxonomy_hierarchical( $taxonomy ) ) { + $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) ); + + $existing_term = null; + if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) { + $existing_term = $name_match; + } elseif ( $slug_match && in_array( $slug, wp_list_pluck( $siblings, 'slug' ) ) ) { + $existing_term = $slug_match; + } + + if ( $existing_term ) { + return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id ); + } + } else { + return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $name_match->term_id ); } - } else { - return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $existing_term->term_id ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index ee770cadc0..81c0ca5da4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.2-beta1-31791'; +$wp_version = '4.2-beta1-31792'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.