Last active
July 23, 2025 14:07
-
-
Save pixelbart/2c4343fa4871b13b9973303c38631cc7 to your computer and use it in GitHub Desktop.
Must-use WordPress plugin to suppress PHP 8+ error reporting for warnings and deprecations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Must-Use Plugin: Frühe Initialisierung & Debug-Handling (Pixelbart) | |
* | |
* Dieses MU-Plugin stellt eine zentrale Grundlage für individuelle WordPress-Projekte unter dem Pixelbart-Setup dar. | |
* Es wird sehr früh geladen und sorgt für stabile Bedingungen – insbesondere in Multi-Plugin-Umgebungen oder | |
* bei eigenem Fehler-Handling. | |
* | |
* Funktionen: | |
* - Bricht sicher ab, wenn WordPress-Kernfunktionen nicht verfügbar sind. | |
* - Passt das Fehlerverhalten bei aktivem WP_DEBUG an (Hinweise und Deprecated-Meldungen unterdrückt). | |
* - Optional: RankMath Action Scheduler kann deaktiviert werden. | |
* - Optional: Action Scheduler Fehler-Logs können aktiviert werden. | |
* | |
* Hinweis: Dieses Plugin wird automatisch geladen und kann nicht über das Backend deaktiviert werden. | |
*/ | |
// Sicherheitsprüfung – nur fortfahren, wenn essentielle Funktionen verfügbar sind | |
if (!function_exists('untrailingslashit') || !defined('WP_PLUGIN_DIR')) { | |
exit; | |
} | |
// === Konfigurierbare Konstanten (am besten in wp-config.php setzen) === | |
defined('PB_DEBUG_SUPPRESS_NOTICES') || define('PB_DEBUG_SUPPRESS_NOTICES', true); // Unterdrückt Notices & Deprecated-Warnungen bei WP_DEBUG | |
defined('PB_DEBUG_DISABLE_RANKMATH_SCHEDULER') || define('PB_DEBUG_DISABLE_RANKMATH_SCHEDULER', true); // Entfernt RankMath Action Scheduler | |
defined('PB_DEBUG_LOG_RANKMATH_REMOVE') || define('PB_DEBUG_LOG_RANKMATH_REMOVE', false); // Loggt Entfernung von RankMath Scheduler | |
defined('PB_DEBUG_LOG_ACTION_SCHEDULER') || define('PB_DEBUG_LOG_ACTION_SCHEDULER', false); // Aktiviert Logging von Action Scheduler Fehlern | |
// === Fehlerbehandlung: Spezifische Notices unterdrücken === | |
if (PB_DEBUG_SUPPRESS_NOTICES) { | |
set_error_handler(function ($errno, $errstr, $errfile, $errline) { | |
if (strpos($errstr, '_load_textdomain_just_in_time') !== false) { | |
return true; | |
} | |
return false; | |
}, E_USER_NOTICE | E_NOTICE); | |
} | |
// === Fehlerlevel für Debug-Modus anpassen === | |
if (defined('WP_DEBUG') && WP_DEBUG === true && PB_DEBUG_SUPPRESS_NOTICES) { | |
error_reporting(E_ALL & ~E_WARNING & ~E_DEPRECATED & ~E_USER_DEPRECATED & ~E_NOTICE); | |
} | |
// === Rank Math: Entferne Action Scheduler (falls gewünscht) === | |
add_action('plugins_loaded', function () { | |
if ( | |
PB_DEBUG_DISABLE_RANKMATH_SCHEDULER && | |
defined('RANK_MATH_FILE') && | |
class_exists('RankMath') | |
) { | |
remove_action('plugins_loaded', ['RankMath\\ActionScheduler\\Initializer', 'init'], 15); | |
if (PB_DEBUG_LOG_RANKMATH_REMOVE) { | |
error_log('Pixelbart MU: Rank Math Action Scheduler wurde entfernt.'); | |
} | |
} | |
}, 5); | |
// === Optionales Logging von Action Scheduler-Fehlern === | |
if (PB_DEBUG_LOG_ACTION_SCHEDULER) { | |
add_action('action_scheduler_failed_execution', function ($action_id, $exception) { | |
error_log("Action Scheduler Fehler bei ID $action_id:"); | |
error_log($exception->getMessage()); | |
error_log(print_r($exception->getTraceAsString(), true)); | |
}, 10, 2); | |
add_action('init', function () { | |
if (class_exists('ActionScheduler')) { | |
$store = ActionScheduler::store(); | |
error_log('Pixelbart MU: Action Scheduler Store ist: ' . get_class($store)); | |
} else { | |
error_log('Pixelbart MU: ActionScheduler-Klasse nicht gefunden.'); | |
} | |
}); | |
} | |
// === Fehlerhafte Aktionen (z. B. ID statt Objekt) vor Ausführung abfangen === | |
add_filter('action_scheduler_before_execute', function ($action) { | |
if (is_int($action)) { | |
// error_log("Pixelbart MU: Aktion mit ID {$action} abgebrochen – ungültiges Objekt."); | |
return false; | |
} | |
return $action; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment