Stop WordPress error notifications to the admin email and redirect them to a specific email address

Introduction

By default, WordPress sends error notifications to the admin email configured in the site settings. However, there are scenarios where you may want these notifications to go to a different email address for monitoring purposes. This guide explains how to redirect these notifications to a specific email using WordPress filters and actions.

Implementation

Step 1: Override the Admin Email

WordPress uses the get_option('admin_email') function to retrieve the admin email. To change this behavior, use the wp_mail filter to redirect notifications to a specific email.

Step 2: Add the Custom Code

Place the following code in your theme's functions.php file or a custom plugin:

                

// Redirect WordPress notification emails to a specific address
function custom_error_notification_email($args) {
    // Specify the email address to receive notifications
    $specific_email = 'error-monitoring@example.com';
    
    // Check if this is an error notification
    if (strpos($args['message'], 'Error') !== false || strpos($args['message'], 'Fatal') !== false) {
        $args['to'] = $specific_email; // Change the recipient email
    }

    return $args;
}
add_filter('wp_mail', 'custom_error_notification_email');

                
            

This code checks if the email message contains error-related keywords like "Error" or "Fatal" and redirects it to a specified email address.

Step 3: Test the Configuration

Trigger a sample error in WordPress (e.g., by deactivating an essential plugin temporarily). Verify that the error notification is sent to the new email address instead of the admin email.

Post a Comment

0 Comments