$wpdb->update or $wpdb->insert results in slashes being added in front of quotes

$wpdb->update or $wpdb->insert results in slashes being added in front of quotes

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Escaped Quotes in $wpdb->update and $wpdb->insert: A Deep Dive into Magic Quotes

If you have recently upgraded your WordPress environment or encountered this issue on a fresh install, you might be seeing single quotes escaped with backslashes when using $wpdb->insert or $wpdb->update. For instance, an input like 'I've eaten strawberries' is being treated as \'I\'ve eaten strawberries\' in your database queries.

The most common advice found online suggests disabling "Magic Quotes" in php.ini or modifying core WordPress files to strip these slashes. However, relying on server-level configuration changes for application logic is generally a bad practice, and editing core files is fragile. The root cause is almost certainly not legacy Magic Quotes behavior but rather an interaction between how data is received, how it's escaped, and how the escaping functions are being applied—or double-applied—in your specific flow.

Understanding Modern PHP and Escaping

In modern PHP versions (5.4+), "Magic Quotes" as a runtime feature that automatically adds slashes to incoming variables have been officially removed. They do not exist in the core language anymore. If you are seeing this behavior today, it is almost certainly due to one of two scenarios:

  1. Legacy Data or Configuration: Your php.ini still has magic_quotes_gpc, magic_quotes_runtime, or magic_quotes_sybase enabled (rare but possible on very old hosting environments), and you haven't disabled them.
  2. Double Escaping Logic: Your application logic is escaping data once, perhaps manually, and then $wpdb->prepare() or the specific $wpdb methods are escaping it again.

The Role of $wpdb->prepare()

The $wpdb class in WordPress is designed to prevent SQL injection primarily through prepared statements. When you use $wpdb->prepare(), the method handles string interpolation safely. However, if you pass data that has already been escaped (e.g., via esc_html(), sanitize_text_field(), or manual slash insertion), and then run it through a query execution context that expects raw input for further escaping, you end up with double-escaped strings.

Consider the scenario where you manually sanitize a value before passing it to $wpdb->insert:

// Incorrect approach leading to potential double escaping issues
$title = esc_sql($_POST['title']); // Often unnecessary if using $wpdb properly
$wpdb->insert('posts', array(
    'post_title' => $title, 
    'post_content' => $_POST['message']
));

If $title contains I've, esc_sql() might not add slashes (it uses addslashes() internally in older WP versions or expects raw input), but if your environment has Magic Quotes enabled, the data arrives as I\'ve. If you then run a query that isn't fully prepared against this pre-escaped string, the database receives I\\'ve, which might resolve to I've depending on driver settings, but often results in syntax errors or stored data with unwanted backslashes.

Diagnosing the Environment

Before modifying core files, verify your environment. Check your php.ini for the following directives:

  • magic_quotes_gpc: Should be Off.
  • magic_quotes_runtime: Should be Off.
  • magic_quotes_sybase: Should be Off (this replaces single quotes with double quotes and adds slashes, a very specific legacy behavior).

You can inspect these in your PHP info file or via code:

phpinfo(); 
// Look for "Magic Quotes GPC" -> No

If you are on a shared hosting environment where you cannot edit php.ini, you may need to wrap the initialization of WordPress with checks that ensure these variables are false, though this is rarely required in 2024.

The Correct Pattern: Rely on $wpdb->prepare()

The most robust solution is to ensure you are strictly using $wpdb->prepare() for all dynamic data. This method handles the escaping correctly based on the data type passed. If you find yourself manually adding slashes or using addslashes(), stop immediately.

Here is the correct implementation pattern:

$id = sanitize_text_field($_POST['id']);
$title = sanitize_text_field($_POST['title']);
$message = sanitize_textarea_field($_POST['message']);

// Use prepare to handle the SQL injection safety and formatting
$wpdb->update('table_name', array(
    'id' => $id, 
    'title' => $title, 
    'message' => $message
), array('id' => $id));

In this pattern, sanitize_text_field() cleans the input (removing HTML, trimming whitespace) but does not add SQL slashes. $wpdb->prepare() (implicitly called by $wpdb->update when using named parameters or positional placeholders in newer WP versions) ensures the data is safely transmitted to the database driver.

Avoiding Core Modifications

Many tutorials suggest editing wp-includes/class-wp-db.php to strip slashes before execution. This is a temporary fix that breaks if WordPress updates its core, introducing security vulnerabilities and compatibility issues. The escaping logic lives in the core for a reason; overriding it assumes you know better than the maintainers regarding database driver quirks (like MySQL vs. MariaDB handling of quotes).

When to Link to Resources

If you are implementing this within a larger Laravel-based stack or need advanced migration strategies, resources from https://laravelcompany.com can offer insights into secure ORM usage and database migrations that parallel the safety principles used in WordPress's $wpdb class. However, for standard WordPress installations, sticking to the built-in sanitize_* functions and $wpdb->prepare() is the standard, supported path.

Summary of Actions

  1. Check php.ini: Ensure all magic_quotes_* settings are disabled.
  2. Sanitize Inputs: Use sanitize_text_field() or sanitize_textarea_field(), not manual slash addition.
  3. Use Prepared Statements: Always rely on $wpdb->prepare() or the parameterized methods of $wpdb.
  4. Leave Core Alone: Do not patch core files to strip slashes; fix the environment or the data flow instead.

By respecting the separation of concerns—where your application sanitizes input and the database layer prepares queries—you eliminate the risk of double-escaping and ensure your data integrity remains intact across different PHP versions and database drivers.

Our network