Escaping Table and Field names with wpdb::prepare() in WordPress 6.1

As part of the WordPress 6.1 release, wpdb::prepare() has been updated so it can escape Identifiers (such as Table and Field names) with the %i placeholder (#52506).

This ensures these values are escaped correctly and don’t lead to SQL Injection Vulnerabilities.

Example

$table = 'my_table';
$field = 'my_field';
$value = 'my_value';

$wpdb->prepare('SELECT * FROM %i WHERE %i = %s', $table, $field, $value);

// Output:
//   SELECT * FROM `my_table` WHERE `my_field` = 'my_value'

While this protects you against SQL Injection, where possible you should limit the values the user (attacker) can choose via an allow-list of trusted values; e.g.

$fields = array(
    'name'    => 'user_nicename',
    'url'     => 'user_url',
    'created' => 'DATE(created)',
  );

$sql .= ' ORDER BY ' . ($fields[$order_field] ?? 'user_login');

Performance Improvement

The change to add support for %i has a small performance improvement, as there is a little bit less Regular Expression work involved (generally the more parameters, the better the improvement).

In the Future

WordPress is looking to use %i in core (#56091).

This change will help developers use the literal-string type for the $query parameter (this is where the $query is written as a developer defined string, and all user values are provided separately).

Props to @davidbaumwald for reviewing this dev note.

#6-1, #dev-notes, #performance, #wpdb

A WordPress Commenter

Recent Posts

WordPress 7.1.3 Maintenance and Security Release

This security and maintenance release features 7 security fixes and 4 bug fixes. Because this…

5 days ago

Performance Chat Summary: 6 October 2026

The full chat log is available beginning here on Slack. WordPress Performance Trac tickets @westonruter…

5 days ago

WordPress Version 7.1.3 Released

On October 6, 2026, WordPress 7.1.3 was released to the public. WordPress Version 7.1.3 Details…

5 days ago

Presence API: What’s new

An update on the Presence API feature plugin, announced in April. Here’s everything that’s shipped…

1 week ago

WordPress 7.1.2 Release

This security release features a fix for a critical severity security vulnerability. Because this is…

3 weeks ago

Performance Chat Summary: 22 September 2026

The full chat log is available beginning here on Slack. WordPress Performance Trac tickets @westonruter…

3 weeks ago