Email Triggers for Simple History

Getting notified when critical plugins are updated is something I sometimes get asked about after a bad plugin update takes place. While there are many ways this can be accomplished, today I’m going to show how to trigger an email notification by hooking into the Simple History plugin.

Simple History is a popular plugin for tracking changes to WordPress. With Simple History running we can check new log entries by hooking into the action named simple_history/log/inserted. To use this WordPress action let’s create a must-use plugin. Place the following code in a file named simple-history-email-hook.php within /wp-content/mu-plugins/.

<?php
/**
 * Plugin Name: Simple History Email Hook
 * Plugin URI: https://austinginder.com
 * Description: Trigger email based on Simply History log entry
 * Version: 1.0.0
 * Author: Austin Ginder
 * Author URI: https://austinginder.com
 * Text Domain: simple-history-email-hook
 */

add_action( 'simple_history/log/inserted', 'simple_history_email_hook', 10 );

function simple_history_email_hook( $context ) {
    $data    = (object) $context;
    $email   = "email@domain.tld";
    $headers = [ "Content-Type: text/html; charset=UTF-8" ];
    if ( $data->_message_key == "plugin_bulk_updated" && $data->plugin_slug == "akismet" ) {
        $message = "Previous version {$data->plugin_prev_version} updated to {$data->plugin_version}";
        wp_mail( $email, "Akismet Updated", $message, $headers );
    }
    if ( $data->_message_key == "plugin_installed" && $data->plugin_slug == "akismet" ) {
        $message = "Installed version {$data->plugin_version}";
        wp_mail( $email, "Akismet Updated", $message, $headers );
    }
}

This code is checking for activities plugin_installed or plugin_bulk_updated from Simply History for a specific plugin. You can refer to their code for a full list of possible plugin activities. This combination is quite powerful. It will only trigger an email if the plugin Akismet is updated or installed. While Akismet is a fairly safe plugin, having this level of granular targeting is perfect for something mission critical like a payment or an e-commerce plugin.

Feel free to use this code as a template for any number of email triggers. Simply set $email to a real email address and configure the email subjects, email messages and activity checks as you wish. Enjoy!