Taxonomy: Return a descriptive WP_Error when a term name exceeds the DB column length - #13346
Taxonomy: Return a descriptive WP_Error when a term name exceeds the DB column length#13346irozum wants to merge 2 commits into
Conversation
…s over the DB column length
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
wp_insert_term()andwp_update_term()never validate the length of the term name before writing it, unlike the equivalent checks already in place forwp_insert_user(),wp_check_comment_data_max_lengths(),register_post_type(), andregister_taxonomy(). When a sanitized name exceeds thewp_terms.namecolumn's length, the create path fails with a genericdb_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, sowp_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 withmb_strlen()— matching the DB column'schar-typed length semantics rather than counting bytes. Bothwp_insert_term()andwp_update_term()now call this right after their existing empty-name checks and return a newterm_name_too_longWP_Errorbefore 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 viamb_substr()forchar-typed columns, sowp_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.