Last active
January 19, 2023 16:02
-
-
Save tripflex/9f0b6703a1d1a2c2972f to your computer and use it in GitHub Desktop.
How to add additional email addresses when email is sent to WordPress admin_email option value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* WARNING: this only checks if the email is being sent to the same email as admin_email option. | |
* If for some reason another email is sent to that same email address, but it's not meant as an "admin email" | |
* this filter will still add those additional emails, just something to keep in mind. | |
*/ | |
add_filter( 'wp_mail', 'my_custom_to_admin_emails' ); | |
/** | |
* Filter WP_Mail Function to Add Multiple Admin Emails | |
* | |
* | |
* | |
* @param array $args A compacted array of wp_mail() arguments, including the "to" email, | |
* subject, message, headers, and attachments values. | |
* | |
* @return array | |
*/ | |
function my_custom_to_admin_emails( $args ) { | |
// This assumes that admin emails are sent with only the admin email | |
// used in the to argument. | |
if( is_array( $args['to'] ) ) return $args; | |
$admin_email = get_option( 'admin_email' ); | |
// Check if admin email is in string, as plugins/themes could have changed format (ie. Administrator <admin@domain.com> ) | |
if( strpos( $args['to'], $admin_email ) !== FALSE ){ | |
// Create array in case there are multiple emails defined in CSV format | |
$emails = explode( ',', $args['to'] ); | |
/** | |
* Add any additional emails to the array | |
* | |
* All email addresses supplied to wp_mail() as the $to parameter must comply with RFC 2822. Some valid examples: | |
* user@example.com | |
* User <user@example.com> | |
*/ | |
$emails[] = 'user@example.com'; | |
$emails[] = 'User <user@example.com>'; | |
$args['to'] = $emails; | |
} | |
return $args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent snippet, thanks a lot!