Created
July 18, 2024 14:56
-
-
Save ipokkel/4ec409f2a564bc63151c81286a444eb4 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* BCC admin emails to different recipients based on user meta. | |
* | |
* 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_bcc_admin_emails_on_user_meta( $headers, $email ) { | |
// Set the meta key for the user meta we should check | |
$meta_key = 'extra_info'; // Replace extra_info with your field name. | |
// BCC checkout emails already going to admin_email. | |
if ( strpos( $email->template, '_admin' ) !== false && strpos( $email->template, 'checkout' ) !== false ) { | |
// Get the user (member) | |
$user = get_user_by( 'email', $email->data['user_email'] ); | |
$meta_data = get_user_meta( $user->ID, $meta_key, true ); | |
// Set BCC values depending on whether the user has data for the meta key or not. | |
if ( empty( $meta_data ) ) { | |
$headers[] = 'Bcc:' . 'otheremail@domain.com'; | |
} else { | |
$headers[] = 'Bcc:' . 'otheremail@domain.com,two@domain.com,three@domain.com'; | |
} | |
return $headers; | |
} | |
} | |
add_filter( 'pmpro_email_headers', 'my_pmpro_bcc_admin_emails_on_user_meta', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment