Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #51731, comment 2


Ignore:
Timestamp:
03/28/2025 07:22:20 AM (20 months ago)
Author:
sahilgidwani

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #51731, comment 2

    initial v1  
    1 Hey @merlijnvanlent ,
    2 
    3 === WordPress Default Behavior ===
    4 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:
    5 
    6 
    7 {{{#!php
    8 <?php
    9 $invalid = validate_active_plugins();
    10 if ( ! empty( $invalid ) ) {
    11         foreach ( $invalid as $plugin_file => $error ) {
    12                 $deactivated_message = sprintf(
    13                         /* translators: 1: Plugin file, 2: Error message. */
    14                         __( 'The plugin %1$s has been deactivated due to an error: %2$s' ),
    15                         '<code>' . esc_html( $plugin_file ) . '</code>',
    16                         esc_html( $error->get_error_message() )
    17                 );
    18                 wp_admin_notice(
    19                         $deactivated_message,
    20                         array(
    21                                 'id'                 => 'message',
    22                                 'additional_classes' => array( 'error' ),
    23                         )
    24                 );
    25         }
    26 }
    27 }}}
    28 
    29 === Custom Handling Options ===
    30 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:
    31 
    32 {{{#!php
    33 <?php
    34 add_action('admin_notices', function () {
    35     if (! is_admin() || ! current_user_can('activate_plugins')) {
    36         return;
    37     }
    38 
    39     $invalid_plugins = validate_active_plugins();
    40 
    41     if (empty($invalid_plugins)) {
    42         return;
    43     }
    44 
    45     foreach ($invalid_plugins as $plugin => $error) {
    46         $error_message = is_wp_error($error) ? $error->get_error_message() : __('Unknown error occurred.', 'text-domain');
    47 
    48         // 1. Admin Notice
    49         echo '<div class="notice notice-error is-dismissible">';
    50         echo '<p><strong>' . esc_html($plugin) . '</strong> has been deactivated. <strong>Reason:</strong> ' . esc_html($error_message) . '</p>';
    51         echo '</div>';
    52 
    53         // 2. Redirect to a New Page
    54         if (!isset($_GET['plugin_error_redirect'])) { // Avoid infinite loops
    55             wp_redirect(admin_url('plugins.php?plugin_error_redirect=1'));
    56             exit;
    57         }
    58 
    59         // 3. Log the Reason
    60         error_log("Plugin Deactivated: $plugin - Reason: $error_message");
    61 
    62         // 4. Send Email Notification
    63         $admin_email = get_option('admin_email');
    64         wp_mail($admin_email, 'Plugin Deactivated', "Plugin: $plugin\nReason: $error_message");
    65 
    66         // 5. Display Modal/Popup (Using JavaScript)
    67         ?>
    68         <script>
    69             document.addEventListener("DOMContentLoaded", function() {
    70                 alert("Plugin <?php echo esc_js($plugin); ?> has been deactivated.\nReason: <?php echo esc_js($error_message); ?>");
    71             });
    72         </script>
    73         <?php
    74     }
    75 });
    76 }}}
    77 
    78 
    79 Let me know if you need any further help!