WordPress Drop-in

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

FileDescriptionLoadedType
advanced-cache.phpAdvanced caching plugin.If WP_CACHE is trueNormal
db.phpCustom database class.on loadNormal
db-error.phpCustom database error message.on errorNormal
install.phpCustom installation script.on installNormal
maintenance.phpCustom maintenance message.on maintenanceNormal
object-cache.phpExternal object cache.on loadNormal
php-error.phpCustom PHP error message.on errorNormal
fatal-error-handler.phpCustom PHP fatal error handler.on errorNormal
sunrise.phpExecuted before Multisite is loaded.If SUNRISE is trueMultisite
blog-deleted.phpCustom site deleted message.on deleted blogMultisite
blog-inactive.phpCustom site inactive message.on inactive blogMultisite
blog-suspended.phpCustom site suspended message.on archived or spammed blogMultisite

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

Your email address will not be published. Required fields are marked *