Skip to content

Instantly share code, notes, and snippets.

@christianwach
Forked from r-a-y/warnings.php
Last active February 1, 2024 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christianwach/7d2bb481f9efe185f2b8 to your computer and use it in GitHub Desktop.
Save christianwach/7d2bb481f9efe185f2b8 to your computer and use it in GitHub Desktop.
A WordPress plugin that suppresses warnings created by various plugins and themes in WordPress.
<?php
/**
* Plugin Name: Suppress Log Warnings
* Plugin URI: https://gist.github.com/christianwach/7d2bb481f9efe185f2b8
* Description: Suppresses annoying warnings generated by various plugins.
* Version: 1.0
*
* Put this plugin in /wp-content/mu-plugins/
*
* @package CMW_Suppress_Log_Warnings
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Suppress Warnings Class.
*
* A class that holds plugin functionality.
*
* @since 1.0
*/
class CMW_Suppress_Log_Warnings {
/**
* Constructor.
*
* @since 1.0
*/
public function __construct() {
// Selectively suppress warnings.
set_error_handler( [ &$this, 'errors_suppress' ] );
// Suppress Akismet logging.
add_filter( 'akismet_debug_log', [ $this, 'akismet_suppress' ] );
// Disable Query Monitor's error handling.
if ( ! defined( 'QM_DISABLE_ERROR_HANDLER' ) ) {
define( 'QM_DISABLE_ERROR_HANDLER', true );
}
}
/**
* Suppress errors generated by specified plugins.
*
* @since 1.0
*
* @param int $errno The error number.
* @param string $errstr The error message.
* @param string $errfile Path to the file that caused the error.
* @return bool True to suppress error reporting; false to use default error handler.
*/
public function errors_suppress( $errno, $errstr, $errfile = '' ) {
// Return true to disable all strict notices.
if ( E_STRICT == $errno ) {
//return true;
}
// Do not display these notices:
$patterns = [
// Annoying CiviCRM Smarty notices.
'uploads/civicrm/templates_c',
'files/civicrm/templates_c',
// Older BuddyPress Group extensions.
'Group_Extension::display',
// Annoying BuddyPress notices.
'bp_setup_current_user was called',
'bp_setup_current_user wurde', // in German
// Annoying bbPress notices.
'bbp_setup_current_user was called',
'bbp_setup_current_user wurde', // in German
// Suppress older widget notices. Comment out to debug.
'The called constructor method for WP_Widget',
'WP_Widget ist seit Version 4.3.0', // in German
];
foreach ( $patterns as $pattern ) {
$pattern = str_replace( [ '/', '\\' ], DIRECTORY_SEPARATOR, $pattern );
if ( false !== strpos( $errstr, $pattern ) ) {
return true;
}
if ( false !== strpos( $errfile, $pattern ) ) {
return true;
}
}
// The path was not found, so report the error.
return false;
}
/**
* Suppress debug logging generated by the Akismet plugin.
*
* @since 1.0
*
* @return bool True to allow error reporting; false to disallow.
*/
public function akismet_suppress() {
return false;
}
}
/**
* Gets a reference to this plugin.
*
* @since 1.0
*
* @return CMW_Suppress_Log_Warnings $plugin The plugin reference.
*/
function cmw_suppress_log_warnings() {
// Return instance.
static $plugin;
if ( ! isset( $plugin ) ) {
$plugin = new CMW_Suppress_Log_Warnings();
}
return $plugin;
}
// Bootstrap plugin.
cmw_suppress_log_warnings();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment