Make WordPress Core

Opened 6 years ago

Last modified 20 months ago

#51731 new feature request

Add the reason for deactivation to the 'deactivate_plugin' hook.

Reported by: merlijnvanlent Owned by:
Priority: normal Milestone: Awaiting Review
Component: Plugins Version:
Severity: normal Keywords: needs-patch
Cc: Focuses: administration

Description

To be able to check if a plugins was disabled by a user action or because of the 'validate_active_plugins' check that is done on when visiting the plugins.php page would be great from a management perspective. Many WordPress installations have some required plugins which are not necessary for the site to work but do need to be active for one reason or another. This enables the option to notify the admin when a plugin gets disabled by accident because of a failed updated or any other reason.

Change History (2)

#1 @johnbillion
6 years ago

  • Keywords needs-patch added
  • Version 5.5.2

This is a good idea.

#2 @sahilgidwani
20 months ago

Hey @merlijnvanlent ,

WordPress Default Behavior

WordPress already handles plugin deactivation due to errors by displaying an admin notice on the plugins.php page. This behavior is implemented in validate_active_plugins() and the relevant section of code is:

<?php
$invalid = validate_active_plugins();
if ( ! empty( $invalid ) ) {
        foreach ( $invalid as $plugin_file => $error ) {
                $deactivated_message = sprintf(
                        /* translators: 1: Plugin file, 2: Error message. */
                        __( 'The plugin %1$s has been deactivated due to an error: %2$s' ),
                        '<code>' . esc_html( $plugin_file ) . '</code>',
                        esc_html( $error->get_error_message() )
                );
                wp_admin_notice(
                        $deactivated_message,
                        array(
                                'id'                 => 'message',
                                'additional_classes' => array( 'error' ),
                        )
                );
        }
}

Custom Handling Options

If a WordPress installation requires custom behavior, such as logging, redirection, email notifications, or displaying modals, a custom implementation can be used. Below is an example of a custom admin notice and logging system:

<?php
add_action('admin_notices', function () {
    if (! is_admin() || ! current_user_can('activate_plugins')) {
        return;
    }

    $invalid_plugins = validate_active_plugins();

    if (empty($invalid_plugins)) {
        return;
    }

    foreach ($invalid_plugins as $plugin => $error) {
        $error_message = is_wp_error($error) ? $error->get_error_message() : __('Unknown error occurred.', 'text-domain');

        // 1. Admin Notice
        echo '<div class="notice notice-error is-dismissible">';
        echo '<p><strong>' . esc_html($plugin) . '</strong> has been deactivated. <strong>Reason:</strong> ' . esc_html($error_message) . '</p>';
        echo '</div>';

        // 2. Redirect to a New Page
        if (!isset($_GET['plugin_error_redirect'])) { // Avoid infinite loops
            wp_redirect(admin_url('plugins.php?plugin_error_redirect=1'));
            exit;
        }

        // 3. Log the Reason
        error_log("Plugin Deactivated: $plugin - Reason: $error_message");

        // 4. Send Email Notification
        $admin_email = get_option('admin_email');
        wp_mail($admin_email, 'Plugin Deactivated', "Plugin: $plugin\nReason: $error_message");

        // 5. Display Modal/Popup (Using JavaScript)
        ?>
        <script>
            document.addEventListener("DOMContentLoaded", function() {
                alert("Plugin <?php echo esc_js($plugin); ?> has been deactivated.\nReason: <?php echo esc_js($error_message); ?>");
            });
        </script>
        <?php
    }
});

Let me know if you need any further help!

Version 0, edited 20 months ago by sahilgidwani (next)
Note: See TracTickets for help on using tickets.