Last active
September 29, 2023 07:54
-
-
Save nicomollet/2109614b12549ad17c422321bf32c592 to your computer and use it in GitHub Desktop.
WordPress: catch plugin deactivation and write log of cause of deactivation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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