Posted on

Silencing Errors from Certain Plugins and Themes in Query Monitor

When a PHP warning or notice occurs during the page load, Query Monitor displays a coloured notification in the admin toolbar that links to the PHP Errors panel. This is great for debugging but can be an annoyance if a third party plugin or theme continually triggers errors that aren’t your responsibility to fix.

Query Monitor allows you to silence errors from specified plugins or themes. Errors will still be shown in the PHP Errors panel but they won’t trigger a coloured notification in the admin toolbar. (Optionally, they can be completely hidden if necessary.)

Here’s how you hide PHP notices from a plugin named foo:

add_filter( 'qm/collect/php_error_levels', function( array $levels ) {
	$levels['plugin']['foo'] = ( E_ALL & ~E_NOTICE );
	return $levels;
} );

This code is hooking into the qm/collect/php_error_levels filter and specifying the error levels which should get reported by Query Monitor for the specified plugin. The error levels are specified using the same bitmask syntax used for PHP’s error_reporting() function, and in this example is telling Query Monitor to report all errors except notices.

The name you use for the plugin array’s index is what Query Monitor shows as the name under the “Component”, for example “Plugin: foo” ends up as “foo”.

You could also tell Query Monitor to only report warnings from your child theme, and completely silence errors from its parent theme (probably not a good idea):

add_filter( 'qm/collect/php_error_levels', function( array $levels ) {
	$levels['theme']['stylesheet'] = ( E_WARNING & E_USER_WARNING );
	$levels['theme']['template']   = ( 0 );
	return $levels;
} );

Any plugin or theme which doesn’t have an error level specified via this filter is assumed to have the default level of E_ALL, which shows all errors.

It’s possible to completely remove silenced errors from Query Monitor’s output, but remember that this may leave you scratching your head if you forget that you’ve put this filter in place:

add_filter( 'qm/collect/hide_silenced_php_errors', '__return_true' );

Finally, if you have special PHP error handling in place on your site and you don’t want Query Monitor to handle errors at all, you can disable the error handling functionality completely:

define( 'QM_DISABLE_ERROR_HANDLER', true );

Leave a Reply

Your email address will not be published. Required fields are marked *