Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/43b985ad2674d499e9e235db286a8f84 to your computer and use it in GitHub Desktop.
Save ipokkel/43b985ad2674d499e9e235db286a8f84 to your computer and use it in GitHub Desktop.
Change the email recipient for a specified set of email templates if the intended recipient has an alternative recipient email address set as user meta.
<?php
/**
* Change the recipient for billing emails to an additional email address.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_change_recipient_for_billing_emails( $user_email, $email ) {
// Set the user meta key for the additional email address.
$email_recipient_meta_key = 'additional_email';
// Set the email templates for which the additional email address should be used.
// https://www.paidmembershipspro.com/documentation/member-communications/list-of-pmpro-email-templates/
$email_templates = array( 'invoice', 'billable_invoice', 'billing', 'billing_failure', 'payment_action', 'credit_card_expiring' );
// Is this an email we should change the recipient for?
if ( in_array( $email->template, $email_templates, true ) ) {
// Get the user
$user = get_user_by( 'email', $user_email );
// Get the additional email address from the usermeta table.
$additional_email = get_user_meta( $user->ID, $email_recipient_meta_key, true );
// Check if additional email is set and is a valid email address.
if ( $additional_email && is_email( $additional_email ) ) {
// Return the additional email address.
return $additional_email;
}
}
return $user_email;
}
add_filter( 'pmpro_email_recipient', 'my_pmpro_change_recipient_for_billing_emails', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment