Make WordPress Core

Opened 7 years ago

Last modified 6 days ago

#46010 new defect (bug)

Auto-generated term slugs can be greater than 200 characters, giving "Could not insert term into the database." error

Reported by: mikejolley's profile mikejolley Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: needs-unit-tests dev-feedback has-patch 2nd-opinion
Focuses: Cc:

Description

When a slug is not user defined, WordPress attempts to generate it from the term name within wp_insert_term. If the slug exists, when inserting subcategories, WordPress prefixes the slug with the top level term name.

https://core.trac.wordpress.org/browser/tags/5.0.2/src/wp-includes/taxonomy.php#L2093

Because of this, long term names can go over the slug length limit and error. WordPress admin simply shows:

Could not insert term into the database.

There is no reasoning provided, and when using wp_insert_term programatically it's hard to know what's at fault.

This issue is more apparent when using cyrillic characters since they are stored encoded in the DB.

Take "Категория на продукта" as an example. In the database this gets stored as:

%d0%ba%d0%b0%d1%82%d0%b5%d0%b3%d0%be%d1%80%d0%b8%d1%8f-%d0%bd%d0%b0-%d0%bf%d1%80%d0%be%d0%b4%d1%83%d0%ba%d1%82%d0%b0

This is 116 characters. The maximum allowed slug length defined in the wp_terms database schema is 200 characters.

Steps to reproduce:

  • Add a top level category "Категория на продукта"
  • Add another "Категория на продукта" term as a subcategory of "Категория на продукта"
  • See error

This is what WordPress attempted to insert into the database:

(
    [name] => Категория на продукта
    [slug] => %d0%ba%d0%b0%d1%82%d0%b5%d0%b3%d0%be%d1%80%d0%b8%d1%8f-%d0%bd%d0%b0-%d0%bf%d1%80%d0%be%d0%b4%d1%83%d0%ba%d1%82%d0%b0-%d0%ba%d0%b0%d1%82%d0%b5%d0%b3%d0%be%d1%80%d0%b8%d1%8f-%d0%bd%d0%b0-%d0%bf%d1%80%d0%be%d0%b4%d1%83%d0%ba%d1%82%d0%b0
    [term_group] => 0
)

The slug is too long. We either need error checking for this, or we need to truncate the slug length to avoid the issue and keep slug < 200 chars.

Attachments (4)

46010.diff (593 bytes) - added by subrataemfluence 7 years ago.
term-insert-error.png (89.2 KB) - added by mindctrl 3 months ago.
Term insert error when length exceeded.
46010.2.diff (3.8 KB) - added by nikunjraj29 2 weeks ago.
The patch ensures that: Auto‑generated slugs are properly truncated to fit within the database’s maximum length. Term insertion no longer fails due to long slugs. Identifying and fixing a core bug that directly impacts database integrity. Contributing a clean, minimal patch that’s easy for reviewers to test and merge.
fix-46010-term-slug.diff (1.1 KB) - added by nikunjraj29 12 days ago.
Uploading refreshed patch: - Cleans up unrelated file changes - Keeps only taxonomy.php modification for truncating slugs over 200 chars This replaces the previous patch attachment. Keywords: has-patch needs-testing

Download all attachments as: .zip

Change History (14)

#1 @swissspidy
7 years ago

  • Keywords needs-patch added

#2 @subrataemfluence
7 years ago

  • Keywords has-patch added; needs-patch removed

#3 @mikejolley
7 years ago

  • Keywords needs-patch added; has-patch removed

@subrataemfluence just a note on that diff, whilst it makes sense in technical terms, a user using cyrillic probably won't understand the length issue as they only typed 21.

I wonder if a better slug generation is needed here, or a fallback.

#4 @subrataemfluence
7 years ago

  • Keywords reporter-feedback added

While generating a slug, option is always there to truncate (let's call this the fallback) it to the maximum allowed characters.

Technically it sounds ok, but not sure whether its a good idea from SEO perspective!

When auto-generated truncate is probably the best option, apart from throwing an error and stopping it from getting added into the database.

Any suggestion on better slug generation would be nice!

#5 @mikejolley
7 years ago

Any suggestion on better slug generation would be nice!

If it cannot prefix the parent term due to length, could it not just append -2, -3 etc. Obviously it's not ideal, but it's better than the error message, and the end-user can edit it if needed manually.

Last edited 7 years ago by mikejolley (previous) (diff)

#6 @swissspidy
7 years ago

  • Keywords needs-unit-tests dev-feedback added; reporter-feedback removed

#7 @callumbw95
5 months ago

  • Keywords close added

Hi All,
I have taken a look at this now that this with the latest version of WordPress and It appears there is a string length limit in place now to stop this from erroring when inserting into the database. It no longer throughs up an error message as before, but instead just truncates behind the scenes without any notification that it has done this.
In regards to the string length due to the encoding, this still appears to be prevalent, but I really cannot think of a solution to get around this issue, and I am not sure that there will be one.
As the main complaint from this ticket looks to be resolved in the latest releases, I believe we can close this ticket down?

@mindctrl
3 months ago

Term insert error when length exceeded.

#8 @mindctrl
3 months ago

Hi all, I'm able to reproduce the issue using the same category names in the ticket description, and the error is still generic.

This ticket was mentioned in PR #10323 on WordPress/wordpress-develop by NikunjRajpara.


4 weeks ago
#9

  • Keywords has-patch added; needs-patch removed

…db insert errors (Ticket #46010)

This PR adds a truncation check to ensure that term slugs longer than 200 characters are properly shortened before insertion.
This prevents database errors that occur when creating categories, tags, or other terms with very long names.

Trac ticket: https://core.trac.wordpress.org/ticket/46010

@nikunjraj29
2 weeks ago

The patch ensures that: Auto‑generated slugs are properly truncated to fit within the database’s maximum length. Term insertion no longer fails due to long slugs. Identifying and fixing a core bug that directly impacts database integrity. Contributing a clean, minimal patch that’s easy for reviewers to test and merge.

@nikunjraj29
12 days ago

Uploading refreshed patch: - Cleans up unrelated file changes - Keeps only taxonomy.php modification for truncating slugs over 200 chars This replaces the previous patch attachment. Keywords: has-patch needs-testing

#10 @mindctrl
6 days ago

  • Keywords 2nd-opinion added; close removed

If taking a truncating approach, I wonder if the logic should go inside of wp_unique_term_slug() instead, which would also cover wp_update_term().

Note: See TracTickets for help on using tickets.