When WordPress debugging is enabled, it logs all PHP errors, warnings, and notices, often including numerous deprecation notices from outdated code or third-party plugins. These can clutter your debug log, making it hard to locate the critical issues. This guide shows you how to create a must-use (MU) plugin to suppress E_DEPRECATED and E_USER_DEPRECATED notices while logging significant errors, resulting in cleaner, more actionable logs.
Step 1: Create the MU Plugin
Must-use (MU) plugins are automatically activated by WordPress and run early, making them ideal for configuring error reporting. To set up the plugin:
- Navigate to
/wp-content/in your WordPress installation. - Create a folder named
mu-pluginsif it doesn’t already exist. - Create a file named
custom-error-handler.phpin/wp-content/mu-plugins/and add the following code:
/**
* Custom Error Handler for WordPress
*
* This MU plugin configures PHP error reporting to control which error types
* are displayed/logged during WordPress execution.
*
* @package WordPress
* @subpackage MU-Plugins
*/
add_action(
'muplugins_loaded',
function ( $a = false ) {
/**
* Configure PHP Error Reporting
*
* ERROR TYPES INCLUDED (Reported):
* - E_ERROR (1): Fatal run-time errors that halt execution. Example: "Fatal error: Call to undefined function some_function()"
* - E_WARNING (2): Non-fatal run-time warnings, script continues. Example: "Warning: Division by zero"
* - E_PARSE (4): Compile-time parse/syntax errors. Example: "Parse error: syntax error, unexpected '}'"
* - E_NOTICE (8): Run-time notices for minor issues. Example: "Notice: Undefined variable: user_name"
* - E_CORE_ERROR (16): Fatal errors during PHP startup. Example: "Fatal error: Allowed memory size exhausted"
* - E_CORE_WARNING (32): Warnings during PHP startup. Example: "Warning: PHP Startup: Unable to load dynamic library"
* - E_COMPILE_ERROR (64): Fatal compile-time errors. Example: "Fatal error: Cannot redeclare class"
* - E_COMPILE_WARNING (128): Non-fatal compile-time warnings
* - E_USER_ERROR (256): User-generated fatal errors via trigger_error()
* - E_USER_WARNING (512): User-generated warnings via trigger_error()
* - E_USER_NOTICE (1024): User-generated notices via trigger_error()
* - E_STRICT (2048): Forward compatibility suggestions. Example: "Strict Standards: Declaration should be compatible"
* - E_RECOVERABLE_ERROR (4096): Catchable fatal errors. Example: "Catchable fatal error: Argument must be instance of User"
*
* ERROR TYPES EXCLUDED (Suppressed):
* - E_DEPRECATED (8192): Notices about deprecated PHP/WordPress functions. Suppressed to avoid noise from legacy code
* - E_USER_DEPRECATED (16384): User-generated deprecation notices. Suppressed to avoid third-party plugin warnings
*/
error_reporting(
E_ALL // Report all error types (value: 32767 - includes all flags)
& ~E_DEPRECATED // EXCEPT deprecated function notices (8192) - suppressed for legacy compatibility
& ~E_USER_DEPRECATED // EXCEPT user-defined deprecation notices (16384) - suppressed to reduce log noise
);
return $a;
},
PHP_INT_MAX, // Ensure our hook runs last
1
);
Step 2: How It Works
The MU plugin uses the muplugins_loaded action hook to configure PHP’s error reporting early in the WordPress execution process, before other plugins or themes load. The error_reporting() function is set to:
- Log all error types
E_ALL, including fatal errorsE_ERROR, parse errorsE_PARSE, warningsE_WARNING, and noticesE_NOTICE. - Suppress
E_DEPRECATEDandE_USER_DEPRECATEDnotices, which are common in legacy code or third-party plugins and often clutter logs.
This ensures that only significant errors are logged to /wp-content/debug.log, making it easier to focus on issues that affect your site’s functionality.
Step 3: Enable WordPress Debugging
To log errors to a file, add or update the following lines of code in your wp-config.php (located in the root of your WordPress installation):
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false); // Prevents errors from appearing on the frontend
These settings save errors to /wp-content/debug.log. You can monitor the log in real-time using a command like tail -f wp-content/debug.log in your terminal. Ensure the /wp-content/ directory is writable by your server.
This MU plugin streamlines WordPress debugging by suppressing deprecation notices, allowing you to focus on critical errors and troubleshoot your site more effectively.