How to stop WordPress from logging deprecation warning notices

WordPress debug log is very useful to developers as it allows to see many issues our code (or third-party code) may be causing. This is often the first point of call when there is a problem on a website — checking the log.

When working with a site that has a lot of plugins, there are times where the log can become difficult to parse visually. Deprecation notices are one thing that occasionally get in the way on a few projects I’ve worked on and when the log is full of deprecation notices, it’s hard to see if/when my code is causing any warnings, notices, or errors.

WordPress uses a collection of functions for deprecating code. These are great because they allow developers to indicate (via the debug.log) that a particular piece of code is deprecated and also specify what to use instead. Within each of these functions there is a filter we can use to prevent the call to PHP’s trigger_error() function which will stop the warning from appearing in our debug.log file. By implementing the snippet below, you’ll be able to stop all deprecation notices passing through WordPress’ core deprecation functions from clogging up the log file.

Be mindful that it is always better to fix any code resulting in a warning or updating and outdated plugins that may be causing it. The following snippet is intended to be a temporary fix to facilitate development.

<?php
// Disable deprecation notices so we can get a better idea of what's going on in our log.
// These hooks are all in wp-includes/functions.php.
// Note that these hooks don't stop WooCommerce from logging deprecation notices on AJAX
// or REST API calls as it makes its own calls to `error_log()` from within
// woocommerce/includes/wc-deprecated-functions.php.
add_filter( 'deprecated_constructor_trigger_error', '__return_false' );
add_filter( 'deprecated_function_trigger_error', '__return_false' );
add_filter( 'deprecated_file_trigger_error', '__return_false' );
add_filter( 'deprecated_argument_trigger_error', '__return_false' );
add_filter( 'deprecated_hook_trigger_error', '__return_false' );

If you drop this snippet into an MU plugin, you’ll ensure it runs nice and early and have the best chance of stopping as many deprecation notices as possible.

A note on WooCommerce’s deprecation notices

Unfortunately, the above strategy won’t work for anything deprecated in WooCommerce when a request is either an AJAX request or a WordPress REST API call. This is because WooCommerce uses its own deprecation functions which contain a condition checking for AJAX and REST API calls. If the current request matches either of these conditions, WooCommerce calls trigger_error() directly and does not provide the same filters for disabling the logged error.

About the author

Phil Kurth is a web developer living in Melbourne, Australia. Phil has a long history of WordPress development and enjoys building tools to empower others in their web design/development practice.

When not working with the web, Phil is usually spending time with his two young sons or is out hiking through the Australian bush.

Good dev stuff, delivered.

Product news, tips, and other cool things we make.

We never share your data. Read our Privacy Policy

© 2016 – 2024 Hookturn Digital. All rights reserved.