Make WordPress Core

Opened 6 weeks ago

Closed 5 weeks ago

Last modified 4 weeks ago

#66185 closed defect (bug) (fixed)

Add NO_BACKSLASH_ESCAPES to wpdb::$incompatible_modes

Reported by: tyxla Owned by: tyxla
Priority: normal Milestone: 7.2
Component: Database Version: trunk
Severity: normal Keywords: has-patch has-unit-tests needs-docs
Cc: Focuses:

Description

When the MySQL/MariaDB NO_BACKSLASH_ESCAPES SQL mode is enabled, the backslash (\) is treated as an ordinary character inside string literals instead of as an escape character.

WordPress assumes backslash escaping in many places, both in core and in the plugin/theme ecosystem. With this mode active, a literal such as 'It\'s' is no longer read as It's. The quote ends the string early, and the query either fails with a syntax error or stores or matches the wrong value.

This is a long-known incompatibility that has come up several times over the years: #2820, #3286, #57149.

wpdb::set_sql_mode() already strips SQL modes that WordPress is known not to support, through wpdb::$incompatible_modes:

protected $incompatible_modes = array(
        'NO_ZERO_DATE',
        'ONLY_FULL_GROUP_BY',
        'STRICT_TRANS_TABLES',
        'STRICT_ALL_TABLES',
        'TRADITIONAL',
        'ANSI',
);

NO_BACKSLASH_ESCAPES is not on that list. If the server or a hosting environment sets it globally, WordPress keeps it for the session, and queries fail.

To fix this, we should add NO_BACKSLASH_ESCAPES to wpdb::$incompatible_modes, so WordPress removes it from the session SQL mode like the other modes it doesn't support. Sites that really need the mode can still re-enable it with the existing incompatible_sql_modes filter.

Change History (7)

#1 @tyxla
6 weeks ago

  • Owner set to tyxla

This ticket was mentioned in ​PR #13758 on ​WordPress/wordpress-develop by ​@tyxla.


6 weeks ago
#2

  • Keywords has-patch has-unit-tests added; needs-patch removed

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

Adds NO_BACKSLASH_ESCAPES to the list of SQL modes that WordPress does not support (wpdb::$incompatible_modes).

When this mode is on, MySQL treats the backslash (\) as a normal character, not as an escape character. WordPress expects backslash escaping, so queries with escaped values can fail. This is a known problem (see #2820, #3286, #57149).

With this change, wpdb::set_sql_mode() removes NO_BACKSLASH_ESCAPES from the session, like it already does for the other unsupported modes. Sites that need the mode can still allow it with the incompatible_sql_modes filter.

The existing test_set_incompatible_sql_mode() test now also checks that NO_BACKSLASH_ESCAPES is removed.

​@therssoftware commented on ​PR #13758:


6 weeks ago
#3

Tested this locally on PHP 8.3 with MySQL 8.0.

  • Before the PR: Calling $wpdb->set_sql_mode( [ 'NO_BACKSLASH_ESCAPES' ] ) left NO_BACKSLASH_ESCAPES active in the session sql_mode.
  • With the PR applied: It gets stripped out from the session by wpdb::set_sql_mode(), matching the other incompatible modes.
  • Also verified that filtering incompatible_sql_modes still allows overriding this if a site explicitly needs it.
  • All 508 tests in tests/phpunit/tests/db.php pass cleanly (727 assertions).

Looks good to merge!

Use of AI Tools

AI assistance: Yes (formatting aid; tests run and verified locally by me).

#4 @SergeyBiryukov
6 weeks ago

  • Milestone Awaiting Review → 7.2

#5 @tyxla
5 weeks ago

  • Resolution → fixed
  • Status assigned → closed

In 64240:

Database: Add NO_BACKSLASH_ESCAPES to wpdb::$incompatible_modes.

When NO_BACKSLASH_ESCAPES is on, MySQL treats the backslash as a normal character instead of an escape character. WordPress relies on backslash escaping, so queries with escaped values can fail.

wpdb::set_sql_mode() now removes this mode from the session, as it already does for the other incompatible modes. Sites that need the mode can still allow it with the incompatible_sql_modes filter.

Includes an update to test_set_incompatible_sql_mode() to cover the new mode.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13758.

Props tyxla, mcsf, therssoftware.
Fixes #66185.

#6 @jorbin
5 weeks ago

  • Keywords needs-docs needs-dev-note added

Thanks @tyxla! As this is a breaking change, this should get announced on make/core to ensure folks are aware. Also, adding needs docs, but I think this might be in hosting docs rather than something the documentation team maintains.

#7 @tyxla
4 weeks ago

  • Keywords needs-dev-note removed

Good call, @jorbin, thanks!

Make post published: ​https://make.wordpress.org/core/2026/10/09/no_backslash_escapes-sql-mode-is-now-turned-off-by-default-in-wordpress-7-2/

Not sure what's the process for the hosting docs though - any recommendations?

Note: See TracTickets for help on using tickets.