Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created July 18, 2024 14:56
Show Gist options
  • Save ipokkel/4ec409f2a564bc63151c81286a444eb4 to your computer and use it in GitHub Desktop.
Save ipokkel/4ec409f2a564bc63151c81286a444eb4 to your computer and use it in GitHub Desktop.
<?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