WordPress.org

Ready to get started?Download WordPress

Forums

Categories with same name and/or slug issue? Solution required. (10 posts)

  1. jasoncarlmorgan
    Member
    Posted 4 years ago #

    Hello Guys, this is my first post.

    Having used these forums time after time for countless problem solving I'm now faced with an issue I cannot resolve.

    My blog requires multiple categories of the same name under different parents, however this is not my issue. My problem is the way wordpress automatically assigns the category name as the slug if the slug field is left blank.

    To further the issue I require to add categories in bulk for which I have an excellent plugin which allows me to add multiple categories and sub categories at once. However the problem is that it refuses to add categories if a category with that name already exists. Following guidance from these forums I have hacked wordpress so that it checks the cat_nicename (slug) for uniqueness instead but the problem is my bulk category plugin doesn't support slug fields so it passes through wordpress asif the slug field is left blank allowing wordpress to automatically set the slug.

    Further more and something strange, if you add a category with the same name as one that already exists via the wordpress admin panel and not via the bulk editor, wordpress automatically assigns that category with a unique slug based on it's parents in a sanatized form. This is great and what I need but for some reason when adding cats through the bulk editor it seems to ignore this function that wordpress has of automatically defining unique slugs.

    Any help would be much appreciated?

    Many Thanks
    Jason

  2. s_ha_dum
    Member
    Posted 4 years ago #

    It sounds as though your problem is the plugin, not WP. It looks like you need to edit, or maybe just update the plugin.

  3. jasoncarlmorgan
    Member
    Posted 4 years ago #

    Yes I agree, the plugin is up to date.

    I was hoping a solution may be found by locating the section of code in the wordpress files that relates to how the slug is created when the form field is left blank.

    For example I'm assuming wordpress checks if the slug field is blank, and if so, uses $cat_name as the entry instead.

    I was hoping to be able to change that to something like rand() to give each category and random but unique slug. The actual name of the slug is irrelevant to me, I just require each to be unique without me having to do it. Obviously I could add one category at a time, specifying unique slugs for each but I require to add hundreds of categories.

    Any further thoughts welcomed?

    Many Thanks.
    Jason.

  4. jasoncarlmorgan
    Member
    Posted 4 years ago #

    It may seem locating the following lines of code in wordpress core may be exactly what I need:

    // If no slug was provided make it empty so that
    // WordPress will generate one.
    if(empty($category_slug)) {
    $category_slug = "";
    }

    Basically wordpress is being told to generate a slug if empty.
    My thoughts were where:
    //
    $category_slug = "";
    //
    modify to:
    //
    $category_slug = "rand()";
    //
    to create a random string for each slug.

    What do you think?
    Many Thanks
    Jason

  5. s_ha_dum
    Member
    Posted 4 years ago #

    If you are thinking editing the core files you are headed down the road to painful upgrades. At least consider hooking a function to, maybe add_category or create_category.

    I wouldn't generate random slugs. Random is more or less meaningless to humans and its hard to remember. I think you'd be better off cooking up some other more meaningful way to generate the slugs, if you don't like how WP does it.

    But again, it looks like the plugin is the problem so why not edit the plugin instead of WP? Sorry if I've misunderstood. I can't tell from your posts which you are planning.

  6. jasoncarlmorgan
    Member
    Posted 4 years ago #

    Thankyou for your reply

    It makes sense to alter the plugin however with amateur knowledge of php that process may require alot of work as I am unsure how much upgrading the plugin may require to intergrate a solution.

    I should define the needs for the solution a little better, sorry:

    Due to the fact I need to add multiple categories of the same name over and over again each "lot" if you like assigned to a different parent,

    for example:

    Parent A
    -- Cat One

    Parent B
    -- Cat One

    Parent C
    -- Cat One

    As you can see I need "Cat One" to repeat multiple times in each "Parent" with the problem being that wordpress automatically assigns cat_name as the category slug when the category field is left blank. - I cannot fill each slug field manually, it needs to be left automatic.

    The actual slug parameter is irrelevant as they will never be used to access the URL by users having to remember it, thus a random string is only neccessary to ensure each category gets a unique slug so that wordpress allows me to add multiples of the same name.

    Many Thanks
    Jason

  7. s_ha_dum
    Member
    Posted 4 years ago #

    It makes sense to alter the plugin however with amateur knowledge of php that process may require alot of work as I am unsure how much upgrading the plugin may require to intergrate a solution.

    You need no more knowledge to alter the plugin than to alter the WP core files.

    ...the problem being that wordpress automatically assigns cat_name as the category slug when the category field is left blank...

    Right. So edit your plugin so that it isn't submitting an empty slug. Prepend the parent category slug or something like that. This is probably not that difficult to accomplish though I can't say for sure as I don't know which plugin you are using.

    The actual slug parameter is irrelevant as they will never be used to access the URL by users having to remember it, thus a random string is only neccessary to ensure each category gets a unique slug so that wordpress allows me to add multiples of the same name.

    Right now this might not matter but you may want to tweak things in the future and you might then find that understandable slugs are easier to work with.

  8. jasoncarlmorgan
    Member
    Posted 4 years ago #

    Hello,

    ah, now I see what your getting at. I never thought of editing the plugin in that way.
    Thankyou for your help, this is good news, gives me something to work on.

    Many Thanks.
    Jason.

  9. harrybargis
    Member
    Posted 4 years ago #

    Instead of editing the plugin to do this isn't there another plugin we can use? Because the last time I started messing around editing plugins I messed up my whole blog so I'm a bit nervous about doing that again! Thanks.

    Harry

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    Which is why lots of developers don't do testing or modifications directly on their websites. Instead they setup a local copy that's accessible via http://localhost.

    If you run a local installation (with a copy of your live site's database if necessary) then you won't have any worries if something goes wrong..

    Then again, if you're not confident in editing php or files, then why do it, and without backing up?..

    If you wish to learn how the code works, then a good approach is to learn by doing, it's how i got what i'm at with my coding abilities. If you don't have any such intention then the question i suppose is, why are you not instead soliciting help from a professional?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags