The delete_plugin WordPress PHP action fires immediately before a plugin deletion attempt.
Usage
add_action('delete_plugin', 'my_custom_function', 10, 1); function my_custom_function($plugin_file) { // your custom code here }
Parameters
$plugin_file
(string) – Path to the plugin file relative to the plugins directory.
More information
See WordPress Developer Resources: delete_plugin
Examples
Log plugin deletion
Log the deleted plugin in a log file.
add_action('delete_plugin', 'log_plugin_deletion', 10, 1); function log_plugin_deletion($plugin_file) { $log_message = "Plugin deleted: " . $plugin_file . "\n"; error_log($log_message, 3, 'plugin_deletion.log'); }
Send email notification
Send an email notification when a plugin is deleted.
add_action('delete_plugin', 'send_plugin_deletion_notification', 10, 1); function send_plugin_deletion_notification($plugin_file) { $to = '[email protected]'; $subject = 'Plugin Deleted'; $message = 'The following plugin was deleted: ' . $plugin_file; wp_mail($to, $subject, $message); }
Prevent specific plugin deletion
Prevent the deletion of a specific plugin.
add_action('delete_plugin', 'prevent_specific_plugin_deletion', 10, 1); function prevent_specific_plugin_deletion($plugin_file) { if ($plugin_file == 'my-specific-plugin/my-plugin.php') { wp_die('You cannot delete this plugin.'); } }
Backup plugin before deletion
Create a backup of the plugin before it’s deleted.
add_action('delete_plugin', 'backup_plugin_before_deletion', 10, 1); function backup_plugin_before_deletion($plugin_file) { $source = WP_PLUGIN_DIR . '/' . $plugin_file; $destination = WP_CONTENT_DIR . '/backups/plugins/' . $plugin_file; copy($source, $destination); }
Add custom action before plugin deletion
Perform a custom action before the plugin is deleted.
add_action('delete_plugin', 'custom_action_before_plugin_deletion', 10, 1); function custom_action_before_plugin_deletion($plugin_file) { // Perform custom action here }