[wp-trac] [WordPress Trac] #51731: Add the reason for deactivation to the 'deactivate_plugin' hook.

WordPress Trac noreply at wordpress.org
Fri Mar 28 07:20:20 UTC 2025


#51731: Add the reason for deactivation to the 'deactivate_plugin' hook.
-----------------------------+------------------------------
 Reporter:  merlijnvanlent   |       Owner:  (none)
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Plugins          |     Version:
 Severity:  normal           |  Resolution:
 Keywords:  needs-patch      |     Focuses:  administration
-----------------------------+------------------------------

Comment (by sahilgidwani):

 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
 <?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
 <?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!

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51731#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list