butlerblog
2/14/2017 - 7:41 PM

Change the wp_mail() "to" address based on subject

Change the wp_mail() "to" address based on subject

<?php

/**
 * This is a filter for wp_mail(). It checks the subject
 * for the presence of a given string (in this case "New
 * User Registration"), and if that returns true it 
 * sets the "to" address/value.
 */
add_filter( 'wp_mail', 'my_wp_mail_filter' );
function my_wp_mail_filter( $args ) {
	// Check the message subject for a known string in the notification email.
	if ( strpos( $args['subject'], 'New User Registration' ) ) {
		// This is the notification email, so change the "to" address.
		$args['to'] = 'alternate@example.com';
	}
	return $args;
}