“Deactivate and Delete” Plugin for WordPress

Have you ever found yourself juggling multiple plugins on your WordPress site, wishing there was an easier way to clean up the clutter? Whether you’re a seasoned webmaster or a blogging newbie, managing plugins can quickly become overwhelming. That’s where the “Deactivate and Delete” plugin comes in, offering a straightforward solution to streamline your WordPress experience.

Simplify Your WordPress Management with the “Deactivate and Delete” Plugin

Imagine this: with just one click, you can deactivate and remove any unnecessary plugin from your site. No more digging through settings or performing multiple steps. This plugin is designed to save you time and effort, allowing you to focus on what truly matters—creating great content and engaging with your audience.

Download plugin (zip) »

“Deactivate and delete” WordPress plugin code

<?php
/*
Plugin Name: Deactivate and Delete
Description: Adds an option to deactivate and delete plugins in one click.
Version: 1.0
Author: WP Simple Hacks
Author URI: https://wpsimplehacks.com
*/

add_filter('bulk_actions-plugins', 'dad_add_bulk_actions');

function dad_add_bulk_actions($bulk_actions) {
    $bulk_actions['deactivate_and_delete'] = 'Deactivate and Delete';
    return $bulk_actions;
}

add_filter('handle_bulk_actions-plugins', 'dad_handle_bulk_actions', 10, 3);

function dad_handle_bulk_actions($redirect_to, $doaction, $plugins) {
    if ($doaction !== 'deactivate_and_delete') {
        return $redirect_to;
    }

    foreach ($plugins as $plugin) {
        deactivate_plugins($plugin);
        delete_plugins(array($plugin));
    }

    $redirect_to = add_query_arg('deactivate_and_delete', '1', $redirect_to);
    return $redirect_to;
}

add_action('admin_notices', 'dad_bulk_action_admin_notice');

function dad_bulk_action_admin_notice() {
    if (!empty($_REQUEST['deactivate_and_delete'])) {
        echo '<div id="message" class="updated notice notice-success is-dismissible"><p>' . __('Selected plugin(s) deactivated and deleted.', 'deactivate-and-delete') . '</p></div>';
    }
}

add_filter('plugin_action_links', 'dad_add_quick_action', 10, 4);

function dad_add_quick_action($actions, $plugin_file, $plugin_data, $context) {
    if (is_plugin_active($plugin_file)) {
        $actions['deactivate_and_delete'] = '<a href="' . wp_nonce_url(admin_url('plugins.php?action=deactivate_and_delete&plugin=' . $plugin_file), 'deactivate_and_delete_plugin_' . $plugin_file) . '" onclick="return confirm('Are you sure you want to deactivate and delete this plugin?')">' . __('Deactivate and Delete', 'deactivate-and-delete') . '</a>';
    }
    return $actions;
}

add_action('admin_init', 'dad_handle_quick_action');

function dad_handle_quick_action() {
    if (isset($_GET['action']) && $_GET['action'] == 'deactivate_and_delete' && isset($_GET['plugin'])) {
        $plugin = $_GET['plugin'];
        check_admin_referer('deactivate_and_delete_plugin_' . $plugin);

        // Deactivate the plugin
        deactivate_plugins($plugin);

        // Delete the plugin
        delete_plugins(array($plugin));

        // Redirect to plugins page with a success message
        wp_redirect(add_query_arg('deactivate_and_delete', '1', admin_url('plugins.php')));
        exit;
    }
}

Testing

1. Activate the Plugin: Go to the WordPress admin dashboard, navigate to the “Plugins” page, and activate the Deactivate and Delete plugin.

2. Test Quick Action: On the “Plugins” page, look for the “Deactivate and Delete” option next to each active plugin. Click it and confirm the plugin is both deactivated and deleted.

This should ensure that the quick menu option properly deactivates and deletes the selected plugin.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top