Skip to content

Taxonomy: Return a descriptive WP_Error when a term name exceeds the DB column length - #13346

Open
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/36610-term-name-length-validation
Open

Taxonomy: Return a descriptive WP_Error when a term name exceeds the DB column length#13346
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/36610-term-name-length-validation

Conversation

@irozum

@irozum irozum commented Aug 31, 2026

Copy link
Copy Markdown

wp_insert_term() and wp_update_term() never validate the length of the term name before writing it, unlike the equivalent checks already in place for wp_insert_user(), wp_check_comment_data_max_lengths(), register_post_type(), and register_taxonomy(). When a sanitized name exceeds the wp_terms.name column's length, the create path fails with a generic db_insert_error ("Could not insert term into the database.") instead of a descriptive error, and the update path is worse: $wpdb->update()'s return value is never checked, so wp_update_term() reports success while silently discarding the change.

This adds a small private helper, _wp_check_term_name_length(), that reads the actual column length via $wpdb->get_col_length( $wpdb->terms, 'name' ) (falling back to the schema default of 200) and compares it against the name's character count with mb_strlen() — matching the DB column's char-typed length semantics rather than counting bytes. Both wp_insert_term() and wp_update_term() now call this right after their existing empty-name checks and return a new term_name_too_long WP_Error before attempting any database write.

The original ticket's reported symptom (multibyte category/tag names getting silently truncated to invalid UTF-8 and losing all their bytes) no longer reproduces on current trunk — strip_invalid_text() now truncates on character boundaries via mb_substr() for char-typed columns, so wp_check_invalid_utf8() never sees a split sequence. What remains, and what this PR fixes, is the missing length validation itself: crossing the limit no longer causes silent data loss, but it still surfaced as an unhelpful low-level DB error (or, on update, as a false "success").

The separate bug where wp_update_term() reports success without saving any change (unrelated to name length) is filed as its own ticket, #66007, and is intentionally out of scope here.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Implementation, tests, and PR description. Reviewed by irozum.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props irozum.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant