It’s probably coming from either your theme or another plugin that’s indirectly related to what your plugin is doing. Something you’re doing is causing the outdated code to execute. For example, this outdated code is hooked into a filter or action that your code causes to fire. There are other possibilities, but that is fairly likely. The trick is isolating that code.
Unless you can figure out a way to consistently cause the error, it’s going to be very difficult to isolate. You could try using the Linux grep command or equivalent search to find “$wpdb->escape” in the files of both themes and plugins folders. You don’t need to find a consistent trigger this way, but it might not work either.
With a consistent trigger, you can reliably narrow down the culprit to the individual module by using the health-check plugin’s troubleshooting tab. When enabled, a version of WP in its default state is created. Restore just your plugin. Unless your own code is at fault, it should not be possible to trigger the error since WP core will not call wpdb::escape(). Restore your theme, then each plugin, one at a time. When you can again trigger the error, the last restored module is at fault.
Thanks for the tips, I’ll give that a go. Not having it consistently show is a bit of a pain.
Take a look through your server error logs to see if anything is registering. That may give you a bit more information.
kjodle’s suggestion gave me another thought. An error backtrace will quite likely give you a better clue to the source by giving you function names to checkout. Grep or similar is much better at finding function declarations than object method calls. If your PHP does not generate its own suitable backtrace, set your own error handler and get the backtrace data with debug_backtrace(). Define WP_DEBUG as true
on wp-config.php in order to see the output in your browser when it occurs.
Example error handler code I found after a quick search: https://stackoverflow.com/questions/1159216/how-can-i-get-php-to-produce-a-backtrace-upon-errors/1159235#1159235
This code could be added to your own plugin.
I’ll keep on working on this to see if I can track it down but thankfully it’s not my plugin that’s causing it, it was a coincidence and the intermittent nature of it meant I couldn’t actually pin it down. but I deactivated my plugin and I’ve just had two instances of the error. (after four hours).