How to log and notify via email when a WordPress plugin is disabled or deleted?

Sometimes, you want to make sure that a specific esssential plugin is never deleted on your WordPress website or at least you want to be notified when ever this happens.

Imagine you have a tax plugin that calculates the tax in your WooCommerce or Easy Digital Downloads shop system. If this plugin is disabled for a reason, all your purchase prices will be miscalculated and it can take a while until you recognize that.

There is no 100% protection to prevent a plugin from being disabled on your WordPress website, but when it happens, you can copy the following code into a mu-plugin like plugin-logger.php and add it to the folder wp-content/mu-plugins

PHP
<?php

/*
 * Plugin Name: Log deleted plugins by WP Staging
 * Plugin URI: https://wp-staging.com
 * Description: Write a log file whenever a plugin is deleted or deactivated plugins.
 *
 * This is a must-use standalone plugin.
 *
 * Author: WP Staging
 * Version: 1.0
 * Author URI: https://wp-staging.com
 */

add_action('deactivated_plugin', function ($plugin_file) {

    $datetime = date('d-m-Y H:i:s');

    $message = "[{$datetime}] Deactivated plugin {$plugin_file}";

    error_log(
        "$message\n",
        3,
        WP_CONTENT_DIR . "/7263767234-deleted-plugins.log"
    );

    wp_mail('youremailaddress@example.com', 'Plugin deactivated on example.com', $message);

}, 20, 2);

add_action('deleted_plugin', function ($plugin_file, $deleted) {

    if ($deleted) {

        $datetime = date('d-m-Y H:i:s');

        $message = "[{$datetime}] Deleted plugin {$plugin_file}";

        error_log(
            "[{$datetime}] Deleted plugin {$plugin_file}\n",
            3,
            WP_CONTENT_DIR . "/7263767234-deleted-plugins.log"
        );

        wp_mail('youremailaddress@example.com', 'Plugin deleted on example.com', $message);
    }

}, 20, 2);

Once any of the installed plugins is either disabled or deleted by the WordPress hooks deleted_plugin or deactivated_plugin you will get an email notification. It will also log the report in the file wp-content//7263767234-deleted-plugins.log.

I recommend to adjust the path and file name for security purposes.

Author: Rene Hermenau

I'm René Hermenau, founder of WP STAGING. I've been building WordPress infrastructure software since 2013 and writing code on GitHub since 2011. My repos live at github.com/rene-hermenau. WP STAGING started as a small developer project solving the same problem I kept hitting on client work: there was no fast, safe way to clone a WordPress site for staging or migration without breaking serialized data, file paths, or media references. Today we are a team of more than 10 people. The free plugin runs on hundreds of thousands of WordPress installations, and the Pro version powers backup, migration, and staging workflows for agencies, hosting platforms, and ecommerce stores. I'm still hands-on with the codebase and technical architecture. Our releases are built as a team, but many of the core architectural decisions are ones I helped design, test, and evolve over the years: how we handle large database exports, how we keep memory usage flat on multi-GB sites, and how we make migrations atomic against partially written tables. "When you touch code, leave it 10% better than before and write a test." If you're stuck on a WP STAGING question, the docs are at wp-staging.com/docs. If you hit a bug, file it on GitHub at github.com/wp-staging. Our team reads everything that lands there.