Created
June 13, 2016 18:24
-
-
Save joshuadavidnelson/6e434be4d9264775738d7da8f225ff74 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 | |
/** | |
* Route the notification to multiple users in a bcc field. | |
* | |
* @see https://joshuadnelson.com/user-dropdown-list-custom-notification-routing-gravity-forms/#comment-12376 | |
* @see https://www.gravityhelp.com/documentation/article/notification/ | |
*/ | |
// NOTE: This is untested code, please use with caution. Test and improve as necessary. | |
add_filter( 'gform_notification_3', 'route_user_email_notification', 10, 3 ); | |
function route_user_email_notification( $notification, $form , $entry ) { | |
// Creat an array to store all the user ids from the checkbox field. Initializing it here in case it's not filled. | |
$bcc_users_array = array(); | |
foreach( $form['fields'] as &$field ) { | |
// Find the right field | |
if( $field['type'] != 'checkbox' || strpos($field['cssClass'], 'user-emails') === false ) | |
continue; | |
// Pull out the field id and the $entry element | |
$field_id = (string) $field['id']; | |
// ------- Since your field is a checkbox, this should return an array, correct? | |
// I'm not 100% sure on the output here, so you might need to debug and modify to build an array of user ids | |
$bcc_users_array = $entry[ $field_id ]; | |
} | |
// Make sure we have an array with values in it | |
if( !empty( $bcc_users_array ) && is_array( $bcc_users_array ) ) { | |
// Initilize this value as an empty string, then we can add to it. | |
$notification[ 'bcc' ] = ''; | |
// Cycle through the users, assuming the values are all user ids | |
foreach( $bcc_users_array as $bcc_user_id ) { | |
// Make sure the user id is a valid int, otherwise skip it. | |
if( !absint( $bcc_user_id ) ) | |
continue; | |
// Grab the email address from the user id, add it to the bcc field | |
$email = get_the_author_meta( 'user_email', $bcc_user_id ); | |
if ( is_email( $email ) ) { | |
$notification[ 'bcc' ] .= $email . ','; | |
} | |
} | |
// Remove the trailing comma | |
$notification[ 'bcc' ] = rtrim( $notification[ 'bcc' ], "," ); | |
} | |
return $notification; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment