Skip to content

Instantly share code, notes, and snippets.

@oh2fih
Last active April 30, 2025 13:55
Show Gist options
  • Save oh2fih/6a35772a4300ff3fd985e1527eb2eccd to your computer and use it in GitHub Desktop.
Save oh2fih/6a35772a4300ff3fd985e1527eb2eccd to your computer and use it in GitHub Desktop.
Suppress certain patterns from WP debug log lines
<?php
/** Suppress certain patterns from WP debug log lines
*
* Here, $suppress_patterns is an array of arrays. Each inner array contains first the error
* level to match and then string parts that should be found (in order) from the log line.
*
* You can place this in wp-config.php after `define('WP_DEBUG_LOG', true );`
*/
// Utility function to check if all parts appear in order and non-overlapping in a message
function message_contains_parts_in_order(string $message, array $parts): bool {
$pos = 0;
foreach ($parts as $part) {
$found = strpos($message, $part, $pos);
if ($found === false) {
return false;
}
$pos = $found + strlen($part);
}
return true;
}
// Custom error handler to suppress specific structured messages by error level and content
function custom_error_handler($errno, $errstr, $errfile, $errline) {
$suppress_patterns = [
[E_USER_NOTICE, 'Function ', ' was called '], // English
[E_USER_NOTICE, 'Funktiota ', ' kutsuttiin '], // Finnish
// You can add patterns for other error levels as well:
// [E_USER_WARNING, 'Deprecated'],
];
foreach ($suppress_patterns as $pattern) {
$pattern_errno = array_shift($pattern); // Take first item as the expected errno
if ($errno === $pattern_errno && message_contains_parts_in_order($errstr, $pattern)) {
return true; // Suppress a matching message
}
}
return false; // Allow other messages
}
// Register early in wp-config.php
set_error_handler('custom_error_handler');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment