Skip to content

Instantly share code, notes, and snippets.

@nicomollet
Last active September 29, 2023 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicomollet/2109614b12549ad17c422321bf32c592 to your computer and use it in GitHub Desktop.
Save nicomollet/2109614b12549ad17c422321bf32c592 to your computer and use it in GitHub Desktop.
WordPress: catch plugin deactivation and write log of cause of deactivation.
<?php
/**
* Catch plugin deactivation and write log of cause of deactivation (manual or error).
*
* Examples:
* [29-Sep-2023 07:40:40 UTC] Plugin antispam-bee/antispam_bee.php was disabled manually.
* [29-Sep-2023 07:47:29 UTC] Plugin antispam-bee/antispam_bee.php was disabled because Plugin file does not exist.
*
* @param string $plugin Path to the plugin file relative to the plugins directory.
* @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network.
*
* @return void
* @author Nicolas Mollet
*/
function catch_plugin_deactivation_cause( string $plugin, bool $network_deactivating ) {
$cause = 'manually';
$result = validate_plugin( $plugin );
if ( is_wp_error( $result ) ) {
$cause = sprintf( 'because %s', $result->get_error_message() );
}
error_log( sprintf( 'Plugin %s was disabled %s', $plugin, $cause ) );
}
add_action( 'deactivate_plugin', 'catch_plugin_deactivation_cause', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment