Forums

[resolved] Duplicate Category Names (5 posts)

  1. laqrhead
    Member
    Posted 6 months ago #

    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.

  2. nicomarie2009
    Member
    Posted 6 months ago #

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

  3. Pierre_02
    Member
    Posted 5 months ago #

    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.

  4. britanik
    Member
    Posted 5 months ago #

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

  5. ZUZ3L
    Member
    Posted 5 days ago #

    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.

Reply

You must log in to post.

About this Topic