The deleted_plugin WordPress PHP action fires immediately after a plugin deletion attempt.
Usage
add_action('deleted_plugin', 'my_custom_function', 10, 2); function my_custom_function($plugin_file, $deleted) { // your custom code here }
Parameters
$plugin_file
(string) – Path to the plugin file relative to the plugins directory.$deleted
(bool) – Whether the plugin deletion was successful.
More information
See WordPress Developer Resources: deleted_plugin
Examples
Log plugin deletion
Log the plugin deletion to a custom log file.
add_action('deleted_plugin', 'log_plugin_deletion', 10, 2); function log_plugin_deletion($plugin_file, $deleted) { if ($deleted) { error_log("Plugin {$plugin_file} was successfully deleted."); } else { error_log("Plugin {$plugin_file} deletion failed."); } }
Send an email notification on plugin deletion
Send an email notification to the site administrator when a plugin is deleted.
add_action('deleted_plugin', 'email_on_plugin_deletion', 10, 2); function email_on_plugin_deletion($plugin_file, $deleted) { $admin_email = get_option('admin_email'); $subject = "Plugin Deletion"; $message = $deleted ? "Plugin {$plugin_file} was deleted." : "Plugin {$plugin_file} deletion failed."; wp_mail($admin_email, $subject, $message); }
Clear cache after plugin deletion
Clear cache after a plugin has been deleted.
add_action('deleted_plugin', 'clear_cache_on_plugin_deletion', 10, 2); function clear_cache_on_plugin_deletion($plugin_file, $deleted) { if ($deleted) { // Replace with your own cache clearing function my_custom_clear_cache(); } }
Show a custom admin notice after plugin deletion
Display a custom admin notice after a plugin has been deleted.
add_action('deleted_plugin', 'admin_notice_on_plugin_deletion', 10, 2); function admin_notice_on_plugin_deletion($plugin_file, $deleted) { if ($deleted) { $message = "Plugin {$plugin_file} was deleted."; } else { $message = "Plugin {$plugin_file} deletion failed."; } set_transient('my_admin_notice', $message, 5); } add_action('admin_notices', 'show_my_admin_notice'); function show_my_admin_notice() { $notice = get_transient('my_admin_notice'); if ($notice) { echo "<div class='notice notice-success is-dismissible'><p>{$notice}</p></div>"; delete_transient('my_admin_notice'); } }
Backup plugin before deletion
Create a backup of the plugin before it’s deleted.
In this example, we first backup the plugin by creating a zip file in a `plugin_backups` directory within the `wp-content` folder. After the plugin is deleted, we remove the backup from the backup directory. Note that you need to replace `my_custom_create_zip` with your own zip creation function.
add_action('delete_plugin', 'backup_plugin_before_deletion', 10, 1); function backup_plugin_before_deletion($plugin_file) { $plugins_dir = WP_PLUGIN_DIR; $backup_dir = WP_CONTENT_DIR . '/plugin_backups'; if (!file_exists($backup_dir)) { mkdir($backup_dir); } $plugin_path = $plugins_dir . '/' . $plugin_file; $backup_path = $backup_dir . '/' . basename($plugin_file, '.php') . '.zip'; // Replace with your own zip creation function my_custom_create_zip($plugin_path, $backup_path); } add_action('deleted_plugin', 'remove_backup_after_deletion', 10, 2); function remove_backup_after_deletion($plugin_file, $deleted) { if ($deleted) { $backup_dir = WP_CONTENT_DIR . '/plugin_backups'; $backup_path = $backup_dir . '/' . basename($plugin_file, '.php') . '.zip'; if (file_exists($backup_path)) { unlink($backup_path); } } }
Log plugin deletion
Log the plugin deletion attempts to a log file.
add_action('deleted_plugin', 'log_plugin_deletion', 10, 2); function log_plugin_deletion($plugin_file, $deleted) { $message = $deleted ? 'successfully deleted' : 'failed to delete'; error_log("Plugin {$plugin_file} {$message} at " . current_time('mysql')); }
Send an email notification
Send an email notification to the site administrator when a plugin is deleted.
add_action('deleted_plugin', 'notify_plugin_deletion', 10, 2); function notify_plugin_deletion($plugin_file, $deleted) { if ($deleted) { $to = get_bloginfo('admin_email'); $subject = "Plugin {$plugin_file} deleted"; $message = "The plugin {$plugin_file} has been deleted on " . current_time('mysql'); wp_mail($to, $subject, $message); } }
Clear cache after plugin deletion
Clear the cache after a plugin is deleted.
add_action('deleted_plugin', 'clear_cache_after_deletion', 10, 2); function clear_cache_after_deletion($plugin_file, $deleted) { if ($deleted) { // Clear your cache here } }
Log failed plugin deletion attempts
Log failed plugin deletion attempts to a log file.
add_action('deleted_plugin', 'log_failed_deletion', 10, 2); function log_failed_deletion($plugin_file, $deleted) { if (!$deleted) { error_log("Failed to delete plugin {$plugin_file} at " . current_time('mysql')); } }
Perform a custom action upon plugin deletion
Perform a custom action when a specific plugin is deleted.
add_action('deleted_plugin', 'custom_action_on_deletion', 10, 2); function custom_action_on_deletion($plugin_file, $deleted) { if ($deleted && $plugin_file === 'your-plugin/your-plugin.php') { // Perform your custom action here } }