You want to hide PHP warnings in specific folders in WordPress. One old plugin or a vendored library fills debug.log or the screen with warnings and deprecation notices you can’t fix. Turning off WP_DEBUG would also hide the warnings from your own code, and those are the ones you need to see.
This snippet registers a PHP error handler. It drops warnings, notices and deprecations whose source file is inside a folder you list. Anything raised anywhere else goes through to PHP’s normal handling, unchanged. It’s a small typed class that uses PHP 8.1 features: readonly promoted properties, named arguments, first-class callable syntax and match.
The snippet
Put it in an mu-plugin, for example wp-content/mu-plugins/0-dpc-quiet-folders.php. The 0- prefix makes it load before the other mu-plugins. It also works in a Scripts Organizer PHP block or your theme’s functions.php, but in those places it starts later (see Gotchas). Change the two example folders to your own.
<?php
/**
* Plugin Name: DPC Quiet Folders
* Description: Silence PHP warnings, notices and deprecations raised by files in specific folders.
*/
if ( ! class_exists( 'Dpc_Quiet_Folders' ) ) {
final class Dpc_Quiet_Folders {
/** @var callable|null Handler that was active before ours. */
private $previous = null;
/**
* @param list<string> $folders Absolute folder paths to silence.
* @param int $levels Bitmask of error levels to silence.
* @param bool $log Write silenced messages to the error log.
*/
public function __construct(
public readonly array $folders,
public readonly int $levels,
public readonly bool $log = false,
) {}
public function register(): void {
$this->previous = set_error_handler( $this->handle( ... ) );
}
public function handle( int $errno, string $errstr, string $errfile = '', int $errline = 0 ): bool {
if ( ( $errno & $this->levels ) && $this->is_quiet( $errfile ) ) {
if ( $this->log ) {
error_log( sprintf( '[dpc quiet] PHP %s: %s in %s on line %d', self::label( $errno ), $errstr, $errfile, $errline ) );
}
return true; // Handled: PHP shows and logs nothing.
}
if ( is_callable( $this->previous ) ) {
return (bool) call_user_func( $this->previous, $errno, $errstr, $errfile, $errline );
}
return false; // Let PHP's standard handler deal with it.
}
public function is_quiet( string $file ): bool {
if ( '' === $file ) {
return false;
}
$file = wp_normalize_path( $file );
foreach ( $this->folders as $folder ) {
if ( str_starts_with( $file, trailingslashit( wp_normalize_path( $folder ) ) ) ) {
return true;
}
}
return false;
}
private static function label( int $errno ): string {
return match ( $errno ) {
E_WARNING, E_USER_WARNING => 'Warning',
E_NOTICE, E_USER_NOTICE => 'Notice',
E_DEPRECATED, E_USER_DEPRECATED => 'Deprecated',
default => 'Error ' . $errno,
};
}
}
}
function dpc_quiet_folders(): Dpc_Quiet_Folders {
static $instance = null;
if ( null === $instance ) {
$instance = new Dpc_Quiet_Folders(
folders: [
WP_PLUGIN_DIR . '/legacy-plugin',
WP_CONTENT_DIR . '/themes/old-theme/vendor',
],
levels: E_WARNING | E_NOTICE | E_DEPRECATED | E_USER_WARNING | E_USER_NOTICE | E_USER_DEPRECATED,
log: false,
);
$instance->register();
}
return $instance;
}
dpc_quiet_folders();
How it works
set_error_handler()gives PHP a callback that runs before its own error handling. The callback is passed as$this->handle( ... ), PHP 8.1’s first-class callable syntax, so you don’t need[ $this, 'handle' ]strings.- PHP passes the file each error came from as
$errfile.is_quiet()normalises that path withwp_normalize_path()and checks it against each folder with a trailing slash. Because of that slash,legacy-plugindoesn’t also matchlegacy-plugin-pro. - If the error level is in
$levelsand the file is in a listed folder, the handler returnstrue. PHP then neither displays nor logs it. - Any other error is passed to the handler that was registered before this one, if there was one. Otherwise the handler returns
falseso PHP handles it normally. Your own warnings still show up as usual. - The constructor uses readonly promoted properties, so the configuration can’t change after the class is built. The named arguments in
dpc_quiet_folders()make each setting clear at the call site. - A static instance makes sure the handler is registered only once, even if the file is included twice.
Customise it
- Only silence deprecations: set
levels: E_DEPRECATED | E_USER_DEPRECATED. This helps when a plugin isn’t ready for a new PHP version yet. - Keep a record: set
log: true. Silenced messages are then written to the error log with a[dpc quiet]prefix, so you can still review them later. - Vendored libraries: you can list a subfolder, such as
.../my-plugin/vendor. That silences the library but not the plugin’s own code.
For log: true, the messages go wherever PHP’s error log points. With WordPress that’s normally wp-content/debug.log when these lines are in wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
If you want to turn errors off for the whole site instead of only some folders, see Hide Errors using wp-config file.
Gotchas
- Load order matters. The handler only catches errors raised after it is registered. Run as an early mu-plugin, it covers every regular plugin. Run from Scripts Organizer or functions.php, it misses warnings that other plugins raise while they load, but it still catches the ones they raise later at runtime.
- WordPress notices come from core files.
_doing_it_wrong()and_deprecated_function()raise their notices fromwp-includes/functions.php, not from the plugin that called them, so the folder check doesn’t match. Use thedoing_it_wrong_trigger_errorordeprecated_function_trigger_errorfilters for those instead. - Fatal errors can’t be silenced. PHP never sends
E_ERRORor parse errors to a user error handler, and that’s what you want. - Another plugin can replace the handler. If a debugging plugin calls
set_error_handler()after this one, its handler runs first. This snippet only passes errors on to the handler that was registered before it. - Silencing hides symptoms. Report the warnings to the plugin author too.
Snippet poll. Should this snippet be in the Scripts Organizer library for one-click import? 10 votes put it there.
0 of 10 votes