Table of Contents
Introduction
Managing plugins in WordPress can become a cumbersome task, especially when you have numerous plugins installed but not in use. Deactivated plugins can pose security risks and occupy unnecessary space on your server. In this article, we’ll show you how to automatically delete deactivated plugins in WordPress using a simple PHP script.
Prerequisites
- Basic understanding of PHP and WordPress functions.
- Access to your WordPress installation files.
Understanding the Code
The following PHP script automates the process of identifying and deleting deactivated plugins in WordPress:
// Load WordPress Environment require_once('wp-load.php'); require_once('wp-admin/includes/plugin.php'); require_once('wp-admin/includes/file.php'); // Initialize the file system global $wp_filesystem; if (empty($wp_filesystem)) { require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'); $wp_filesystem = new WP_Filesystem_Direct(new StdClass); } function delete_deactivated_plugins() { // Get all installed plugins $all_plugins = get_plugins(); foreach ($all_plugins as $plugin_slug => $plugin_data) { // Check if the plugin is inactive if (!is_plugin_active($plugin_slug)) { // Delete the inactive plugin if (file_exists(WP_PLUGIN_DIR . '/' . $plugin_slug)) { delete_plugins(array($plugin_slug)); echo "Deleted inactive plugin: " . $plugin_data['Name'] . "<br>"; } else { echo "Plugin files not found for: " . $plugin_data['Name'] . "<br>"; } } } } // Call the function delete_deactivated_plugins();
Loading the WordPress Environment:
The script begins by loading the WordPress environment using wp-load.php and including necessary files for managing plugins and files in WordPress.
Initializing the File System:
WordPress provides a global $wp_filesystem object to handle file operations. If it’s not already initialized, the script sets it up using the WP_Filesystem_Direct class.
Function to Delete Deactivated Plugins:
- The delete_deactivated_plugins() function does the heavy lifting. It first retrieves all installed plugins using the get_plugins() function.
- The script then iterates through each plugin and checks if it’s inactive using the is_plugin_active() function.
- If a plugin is inactive, the script attempts to delete it using delete_plugins() and provides feedback on whether the deletion was successful or if the plugin files were not found.
Executing the Function:
Finally, the delete_deactivated_plugins() function is called, which triggers the entire process.
How to Use This Script
Create a PHP File:
Save the provided code in a .php file (e.g., delete-inactive-plugins.php).
Upload the File:
Upload this file to the root directory of your WordPress installation.
Run the Script:
Access the script via your browser (e.g., http://yourwebsite.com/delete-inactive-plugins.php). This will execute the code and delete any inactive plugins.
Delete the Script:
After running the script, it’s advisable to delete the file from your server to prevent unauthorized access.