WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Duplicate Category Names

[Resolved] Duplicate Category Names

  • I was having some issues adding sub-categories that had duplicate names, but unique slugs. I don’t know if this is a bug or a feature, but since the slug is the unique key, it doesn’t make sense to me why you couldn’t be able to have duplicate names if the slug is unique. Anyways, here is a hack, not a plugin, so you’ll need to fix it again if you update wordpress.

    To allow duplicate names, but keep unique slugs, find the following code (lines 312-318) in wp-admin/admin-ajax.php:

    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();
    	}

    Replace with this code:

    if ( category_exists( trim( $_POST['category_nicename'] ) ) ) {
    		$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' => 'category_nicename' ) ),
    		) );
    		$x->send();
    	}

    What you are doing is changing the instances of “cat_name” with “category_nicename”, This has it check the slug for uniqueness, and not the name.

    I tested briefly for myself and it seems to be working.

Viewing 6 replies - 1 through 6 (of 6 total)
  • nicomarie2009
    Member

    @nicomarie2009

    hey, i just made my user in the forum for thanks you, it works and you save me.
    So thank you very much

    Pierre_02
    Participant

    @pierre_02

    and by replacing :

    $cat_id = wp_create_category( $cat_name, $parent );

    by :

    $cat_id = wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );

    Line 241 in the same file you will be able to add categories on the fly in the “post-new” part 😉

    Amicably,

    Pierre.

    britanik
    Participant

    @britanik

    Can you help with the same issue but regarding to POST names instead of CAT names ?

    Is it possible to hack WP so that slugs aren’t unique? I use slugs in a quite strange way. I display slugs as prices on a menu card for a bar.
    Maybe I should find another way.

    THANK YOU, THANK YOU, THANK YOU!!! This is the only place on the whole internet that there seems to be a fix!! Worked for me 🙂

    javierfaus
    Member

    @javierfaus

    That worked out like a charm in WP 2.6.3!!

    Thanks indeed.

    javier.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Duplicate Category Names’ is closed to new replies.