Summary: Learning about the WordPress Drop-ins.
WordPress drop-in plugins are a hidden feature of WordPress used to replace, add, or enhance a limited set of advanced core WordPress features. They are only created by developers or other regular plugins. WordPress drop-in plugins are not regular plugins.
Drop-in plugins are special files that each have a singular and unique purpose within WordPress. For example, you can use a drop-in plugin to replace the core WordPress db.php file and it’s wpdb
class. You could also use the advanced-cache.php WordPress drop-in to add “Advanced Caching” to your website (how the caching works is entirely up to the developer, there are no training wheels with drop-ins). Basically each drop-in has its own purpose and each drop-in works in a different way.
All Supported WordPress Drop-ins
File | Description | Loaded | Type |
---|---|---|---|
advanced-cache.php | Advanced caching plugin. | If WP_CACHE is true | Normal |
db.php | Custom database class. | on load | Normal |
db-error.php | Custom database error message. | on error | Normal |
install.php | Custom installation script. | on install | Normal |
maintenance.php | Custom maintenance message. | on maintenance | Normal |
object-cache.php | External object cache. | on load | Normal |
php-error.php | Custom PHP error message. | on error | Normal |
fatal-error-handler.php | Custom PHP fatal error handler. | on error | Normal |
sunrise.php | Executed before Multisite is loaded. | If SUNRISE is true | Multisite |
blog-deleted.php | Custom site deleted message. | on deleted blog | Multisite |
blog-inactive.php | Custom site inactive message. | on inactive blog | Multisite |
blog-suspended.php | Custom site suspended message. | on archived or spammed blog | Multisite |
Example Drop-in for replacing the fatal-error-handler.php
this file will be needed to be created in the wp-content
folder of our WordPress installation with the name fatal-error-handler.php
this drop-in display user friendly errors to the users.
<?php
/**
* Custom Fatal Error Handler for WordPress
*
* This file replaces the default WordPress fatal error handler.
*/
// Check if the drop-in is being accessed directly
if (!defined('WPINC')) {
die;
}
// Custom Error Handler Class
class Custom_Fatal_Error_Handler {
public function __construct() {
register_shutdown_function([$this, 'handle_fatal_error']);
}
public function handle_fatal_error() {
$error = error_get_last();
if ($error && in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE])) {
// Handle the fatal error
$this->log_error($error);
// Display custom error message to the user
$this->display_custom_error_message();
}
}
private function log_error($error) {
$log_message = sprintf(
"[%s] Fatal error in file %s on line %d: %s\n",
date('Y-m-d H:i:s'),
$error['file'],
$error['line'],
$error['message']
);
// Log the error to a file
error_log($log_message, 3, WP_CONTENT_DIR . '/fatal-error-log.txt');
}
private function display_custom_error_message() {
if (!headers_sent()) {
header("HTTP/1.1 500 Internal Server Error");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Site Error</title>
</head>
<body>
<h1>Something went wrong.</h1>
<p>We are experiencing technical difficulties. Please try again later.</p>
</body>
</html>
<?php
exit;
}
}
// Instantiate the custom error handler
new Custom_Fatal_Error_Handler();
Leave a Reply