Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • WordPress 3.6: Fixing wpdb::escape Deprecation Errors

WordPress 3.6: Fixing wpdb::escape Deprecation Errors

6

WordPress v3.6 was recently released, and as such, not all themes and plugins have been updated to support the new version. If you have PHP error_reporting turned on with E_NOTICES enabled, you are likely to see errors like “Notice: wpdb::escape is deprecated since version 3.6! Use wpdb::prepare() or esc_sql() instead.”

These errors are common with third-party themes and plugins – especially ones that haven’t been updated in a while.

The Cause:

The error occurs because a function used to “escape” strings before being stored in the database has been deprecated. Escaping is the process of removing characters that can lead to SQL injection and XSS. The existing escape() function was deprecated in WordPress v3.6 RC2 and missed the previous released, and so may have gone unnoticed.

The reason for this deprecation was security related, so could not be avoided. The wpdb:escape() function (found in wp-includes/wp-db.php) has been replaces with the newer esc_sql() function.

The Fix:

In the short-term, you can fix the problem yourself. There are two possible solutions:

1. Update PHP Settings (Quick)

Update your PHP init settings to stop reporting E_NOTICES errors. This option is not recommended, as it’s just masking the errors and not really fixing them. Since the error is just a notice, it’s not going to break anything and it’s the quickest way to fix your WordPress site.

However, this won’t be possible for everyone to do, as you need the ability to change your PHP settings – something that is not possible on share hosting providers. If you are on a VPS or dedicated hosting provider, this is a very quick fix for you.

2. Fix the Theme or Plugins Manually (Recommended)

This is the longest but also the recommended option. You basically have to manually go though all your plugins or themes to look for references to $wpdb->escape or $this->wpdb->escape and replace all instances with esc_sql. It’s a straightforward find-and-replace, but it just means going through a lot of files.

If you’re on Linux or Mac, you can use the following command line in your WordPress directory to quickly see which files need to be updated:

Running the command in terminal will give you a list of files that are affected:

grep command to find wpdb-escape

In my example above, you can see that only one plugin needs to be updated, and that’s the Akismet plugin. Opening these files and doing a find-and-replace is enough to fix the issue. If we look at the first line in the search, we need to turn $type  = $wpdb->escape( $type ); into $type  = esc_sql( $type );

Or simply replace $wpdb->escape with esc_sql.

Bug, Deprecation, WordPress

6 comments on “WordPress 3.6: Fixing wpdb::escape Deprecation Errors”

  1. Horst Graebner says:
    August 9, 2013 at 4:24 PM

    Thanks, this helpful tip worked 4 me!

    Reply
  2. Lemon says:
    August 22, 2013 at 10:59 AM

    Life saver, thank you!

    Reply
  3. Scott Glime says:
    June 25, 2014 at 7:45 PM

    Worked like a charm for me. Thank you!

    Reply
  4. Ian Armstrong says:
    August 21, 2014 at 8:27 PM

    It’s amazing to me that major plugins (looking at you Backup Buddy) still haven’t resolved this a year later.

    Reply
  5. sistaidan says:
    March 24, 2016 at 10:03 PM

    Hi guys, Thanks for the tip, looked around for hours but the problem is that it worked perfectly in one file but not in an other. I have now a fatal error : Call to undefined function esc_sql() in… Tryed manythings in vain; I’m a zero in PHP and not so good in english. Could you help me please. All my sites are crashed for hours.
    Thanks a lot

    Reply
    • Niraj Shah says:
      March 30, 2016 at 10:36 AM

      Unfortunately, you either need to update all the plugins / themes to the latest version and hope that it fixes the issue, or you’ll need to manually go and edit each of the affected files. Make sure you back-up any plugins / files before you edit, so you can revert if something goes wrong.

      Reply

Leave a Reply to LemonCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy