Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#21093 closed defect (bug) (duplicate)

wp_unique_term_slug() & Custom Post Type / Page Slugs Comparison

Reported by: wpsmith's profile wpsmith Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Taxonomy Keywords: has-patch
Focuses: Cc:


Let's consider pages and a custom taxonomy registered as follows:

$labels = array(
    'name' => _x( 'Worksheets', 'taxonomy general name' ),
    'singular_name' => _x( 'Worksheet', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Worksheets' ),
    'all_items' => __( 'All Worksheets' ),
    'parent_item' => __( 'Parent Worksheet' ),
    'parent_item_colon' => __( 'Parent Worksheet:' ),
    'edit_item' => __( 'Edit Worksheet' ), 
    'update_item' => __( 'Update Worksheet' ),
    'add_new_item' => __( 'Add New Worksheet' ),
    'new_item_name' => __( 'New Worksheet Name' ),
    'menu_name' => __( 'Worksheet' ),

    array( 'k12_worksheets' ),
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => '', 'with_front' => false, 'hierarchical' => true, ),

Now page slugs can be filtered with wp_unique_post_slug_is_bad_hierarchical_slug (and non-hierarchical custom post types with wp_unique_post_slug_is_bad_flat_slug). For example:

add_filter( 'wp_unique_post_slug_is_bad_hierarchical_slug', 'k12_is_bad_slug', 10, 2 );
add_filter( 'wp_unique_post_slug_is_bad_flat_slug', 'k12_is_bad_slug', 10, 2 );
 * Checks Custom Taxonomy (k12_worksheet_type) for slug conflicts
 * @param boolean $is_bad_slug False as default
 * @param string $slug Slug for "post" of whatever post type
 * @return boolean $is_bad_slug True if bad slug, False if good slug
function k12_is_bad_slug( $is_bad_slug, $slug ) {
   if ( get_terms( 'k12_worksheet_type', array( 'hide_empty' => false, 'slug' => $slug ) ) || 'worksheet' == $slug || 'worksheets' == $slug )
      return true;
   return $is_bad_slug;

However, if a term is created, there is no filter to check the DB for page slugs (etc.). wp_unique_term_slug() "will make slug unique, if it isn't already." So, it would be a good add to do likewise (wp_unique_post_slug_is_bad_hierarchical_slug & wp_unique_post_slug_is_bad_flat_slug).

The obvious impact is that it will prevent possible conflicts and reduce troubleshooting issues. For example, I have a page called Worksheets (, and then have a term from k12_worksheet_type called Worksheets (, the slugs of both are the same with the permalink %post-name%.

Attachments (1)

wp_unique_term_slug.patch (1.7 KB) - added by wpsmith 10 years ago.
First pass

Download all attachments as: .zip

Change History (3)

10 years ago

First pass

#1 @wpsmith
10 years ago

  • Keywords has-patch dev-feedback 2nd-opinion added

#2 @coffee2code
10 years ago

  • Keywords dev-feedback 2nd-opinion removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version set to 3.4

You went a little around the block at first with the description so I didn't know what you were getting at until the patch. I'm going to close this as a dupe of #20783. I amended the existing patch there to incorporate your suggestion of the *_is_bad_* filters to better align with wp_unique_post_slug(). I corrected the incorrect logic in your patch where the first wp_unique_is_bad_term_slug filter was concerned (and changed it to be wp_unique_term_slug_is_bad_hierarchical_slug as is the case for the post version). That ticket already proposed the wp_unique_term_slug filter, and also adds a pre_wp_unique_term_slug.

Feel free to continue discussion there.

Note: See TracTickets for help on using tickets.