Gibt es einen Haken, mit dem ich mehrere E-Mail-Adressen für die Standard-E-Mail-Benachrichtigungen des Administrators senden kann?
Ich hatte gehofft, ich könnte ein Array bauen:
$adminEmails = array('[email protected]', '[email protected]');
Dann lassen Sie alle Admin-E-Mails (wie Benachrichtigungen für neue Benutzer) an $ adminEmails senden
Möglich?
Versuche dies:
update_option( 'admin_email', '[email protected], [email protected]' );
Beachten Sie, dass der Wert eine Zeichenfolge ist. Nur Anführungszeichen öffnen und schließen!
Dies kann erreicht werden, indem die Funktion wp_mail
gefiltert wird, überprüft wird, ob für to
die Administrator-E-Mail festgelegt ist, und in diesem Fall Ihre zusätzlichen E-Mail-Adressen hinzugefügt und die Argumente an wp_mail
zurückgegeben werden.
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 ) {
// If to isn't set (who knows why it wouldn't) return args
if( ! isset($args['to']) || empty($args['to']) ) return $args;
// If TO is an array of emails, means it's probably not an admin email
if( is_array( $args['to'] ) ) return $args;
$admin_email = get_option( 'admin_email' );
// Check if admin email found in string, as TO could be formatted like 'Administrator <[email protected]>',
// and if we specifically check if it's just the email, we may miss some admin emails.
if( strpos( $args['to'], $admin_email ) !== FALSE ){
// Set the TO array key equal to the existing admin email, plus any additional emails
//
// All email addresses supplied to wp_mail() as the $to parameter must comply with RFC 2822. Some valid examples:
// [email protected]
// User <[email protected]>
$args['to'] = array( $args['to'], '[email protected]', 'Admin4 <[email protected]>' );
}
return $args;
}
Wir geben das TO als Array zurück, da wp_mail
das Array behandelt und es nach Bedarf auflöst, um die E-Mail zu senden
Hier ist meine Lösung, sie verwendet den Filter update_option_ *. Ich glaube, dass dies der richtige Weg ist, um hierher zu gelangen. Fügen Sie dies zu einem Plugin oder Ihrer theme functions.php-Datei hinzu, und Sie können dann kommagetrennte Admin-E-Mails sicher im Fenster settings-> general ablegen.
add_filter('pre_update_option_admin_email','sanitize_multiple_emails',10,2);
function sanitize_multiple_emails($value,$oldValue)
{
//if anything is fishy, just trust wp to keep on as it would.
if(!isset($_POST["admin_email"]))
return $value;
$result = "";
$emails = explode(",",$_POST["admin_email"]);
foreach($emails as $email)
{
$email = trim($email);
$email = sanitize_email( $email );
//again, something wrong? let wp keep at it.
if(!is_email($email))
return $value;
$result .= $email.",";
}
if(strlen($result == ""))
return $value;
$result = substr($result,0,-1);
return $result;
}