Skip to content

Comments: Avoid a database error in the duplicate check for four-byte characters - #16231

Open
ankit-addweb wants to merge 4 commits into
WordPress:trunkfrom
ankit-addweb:fix/46947-comment-dupe-check-emoji
Open

ankit-addweb wants to merge 4 commits into
WordPress:trunkfrom
ankit-addweb:fix/46947-comment-dupe-check-emoji

Conversation

@ankit-addweb

Copy link
Copy Markdown

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

What

In wp_allow_comment(), strip the characters the comment_content column cannot store from the value used in the duplicate check, using $wpdb->strip_invalid_text_for_column(). This is the same step wpdb applies when the comment is saved.

Why

With a legacy utf8 comment_content column and a utf8mb4 connection, the duplicate check query fails if the comment contains four-byte characters such as emoji:

Illegal mix of collations (utf8mb3_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,COERCIBLE) for operation '='

On MySQL 9.x the same failure is worded Cannot convert string '...' from utf8mb4 to utf8mb3.

wp_new_comment() already strips the text and calls wp_allow_comment() again after the insert fails, so the comment is still saved without the emoji. The error is logged and the first duplicate check never worked correctly. Comparing the value that would actually be stored avoids the error and makes the first check meaningful.

Reproduced on trunk by @Sukhendu2002 (see the ticket). I confirmed the error message above on MySQL 8.0.44 and the Cannot convert wording on MySQL 9.7.4. I did not test MariaDB or MySQL 5.7.

The change only affects columns that cannot store four-byte characters. utf8mb4 columns are unchanged.

Tests

Adds tests to Tests_Comment_WpAllowComment, each run against a legacy utf8 column and a utf8mb4 column on a utf8mb4 connection:

  • no database error is produced by the duplicate check;
  • a duplicate comment containing four-byte characters is still rejected.

The two legacy-column tests fail without the change and pass with it.

How to test

  1. Use a database where comment_content is utf8/utf8_general_ci and the connection is utf8mb4.
  2. Submit a comment containing ❤🔥💯🙌.
  3. Without the patch a database error is logged. With it, there is none.
  4. npm run test:php -- --filter Tests_Comment_WpAllowComment

… characters.

When the `comment_content` column uses the legacy `utf8` character set and the connection uses `utf8mb4`, the duplicate comment check in `wp_allow_comment()` failed with a database error if the comment contained four-byte characters such as emoji, for example "Illegal mix of collations (utf8mb3_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,COERCIBLE) for operation '='".

Strip the characters the column cannot store from the value being compared, in the same way `wpdb` does when the comment is saved. This avoids the error and means the value compared matches the value that was stored.

See #46947.
@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.

@ankit-addweb
ankit-addweb marked this pull request as ready for review November 25, 2026 12:03
@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.

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

Props ankitp47.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

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