Make WordPress Core

Opened 16 years ago

Closed 16 years ago

Last modified 6 years ago

#9335 closed defect (bug) (fixed)

Child categories error out as duplicates even if slugs and parent categories are different

Reported by: mofoq's profile mofoq Owned by:
Milestone: 2.8 Priority: normal
Severity: normal Version: 2.7.1
Component: Administration Keywords:
Focuses: Cc:

Description

In short, child (sub) categories seem to be not allowed and error out as duplicates if another exists with that name, even though the slug and parent categories are different.

Here's an example:
Child category: Accessories
Parent category: PC
Slug: pcaccessories

Child category: Accessories
Parent category: Mac
Slug: macaccessories

It is related to a previously reported ticket (#6542) which was closed, siting a "duplicate" but it is not a duplicate issue as slugs are unique, yet it complains.

I recommend that the code in Changeset #7336 (http://core.trac.wordpress.org/changeset/7336) not check for the existence of an identical category but rather, an identical slug.

(By the way, I'm using the latest stable release, 2.7.1)

Change History (8)

#1 follow-up: @zeronex
16 years ago

  • Milestone set to 2.8

The word around http://core.trac.wordpress.org/ticket/6542#comment:2 works i.e.

  1. create the second category with different and slug.
  2. Edit the second one and correct category name and will work.

But waste of time and should work at the first place if the slug and level(parent) is different.

#2 in reply to: ↑ 1 ; follow-up: @mofoq
16 years ago

Replying to zeronex:

I tried using an unique slug but it didn't work at least not when creating a new one.
I haven't tried "editing" an existing one though.

instead, I ended up making a minor "adjustment" in admin-ajax.php, line 312

	if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
		$x = new WP_Ajax_Response( array(
			'what' => 'cat',
			'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
		) );
		$x->send();
	}

to

	if ( !empty(get_category_by_slug(trim( $_POST['category_nicename']))->cat_name) ) { 
		$x = new WP_Ajax_Response( array(
			'what' => 'cat',
			'id' => new WP_Error( 'cat_exists', __('The category you are trying to create has a slug that already exists. Please use an unique slug.' ), array( 'form-field' => 'cat_name' ) ),
		) );
		$x->send();
	}

it basically uses the slug instead and checks to see if it can get a category from it (it might be better to change it to check if a cat_id is non-empty instead of cat_name)

#3 in reply to: ↑ 2 ; follow-up: @zeronex
16 years ago

Replying to mofoq:

Replying to zeronex:

I tried using an unique slug but it didn't work at least not when creating a new one.
I haven't tried "editing" an existing one though.

instead, I ended up making a minor "adjustment" in admin-ajax.php, line 312

	if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
		$x = new WP_Ajax_Response( array(
			'what' => 'cat',
			'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
		) );
		$x->send();
	}

to

	if ( !empty(get_category_by_slug(trim( $_POST['category_nicename']))->cat_name) ) { 
		$x = new WP_Ajax_Response( array(
			'what' => 'cat',
			'id' => new WP_Error( 'cat_exists', __('The category you are trying to create has a slug that already exists. Please use an unique slug.' ), array( 'form-field' => 'cat_name' ) ),
		) );
		$x->send();
	}

it basically uses the slug instead and checks to see if it can get a category from it (it might be better to change it to check if a cat_id is non-empty instead of cat_name)

I think categories with similar name should only be allowed if the parents are different and have different slugs. Well even if slugs are same URL will be different.

#4 in reply to: ↑ 3 @mofoq
16 years ago

Replying to zeronex:

I think categories with similar name should only be allowed if the parents are different and have different slugs. Well even if slugs are same URL will be different.

I agree, but I was just a tad too lazy to implement that fully :p
I hope that the full fix that gets implemented does just this.

One thing I do foresee is the possible conflicts with some internal functions that rely solely on the cat_name instead of the ID (for instance: get_cat_ID() ).

#5 follow-up: @Viper007Bond
16 years ago

Just a thought: what were to happen if you were to edit one of these categories and change it's parent so a collision occurred?

#6 in reply to: ↑ 5 @zeronex
16 years ago

Replying to Viper007Bond:

Just a thought: what were to happen if you were to edit one of these categories and change it's parent so a collision occurred?

You have a good point but I think no one would like this bug, so similar checks should be in place when creating/updating any category.

#7 @ryan
16 years ago

  • Resolution set to fixed
  • Status changed from new to closed

Fixed with [10905]

This ticket was mentioned in Slack in #core-editor by benoitchantre. View the logs.


6 years ago

Note: See TracTickets for help on using tickets.