WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 9 months 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 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: needs-patch needs-unit-tests dev-feedback
Focuses: Cc:
PR Number:

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 (1)

46010.diff (593 bytes) - added by subrataemfluence 9 months ago.

Download all attachments as: .zip

Change History (7)

#1 @swissspidy
9 months ago

  • Keywords needs-patch added

#2 @subrataemfluence
9 months ago

  • Keywords has-patch added; needs-patch removed

#3 @mikejolley
9 months 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
9 months 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
9 months 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 9 months ago by mikejolley (previous) (diff)

#6 @swissspidy
9 months ago

  • Keywords needs-unit-tests dev-feedback added; reporter-feedback removed
Note: See TracTickets for help on using tickets.