Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshuadavidnelson/1c0f0dccfb56cc3c25187876c82be7fb to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/1c0f0dccfb56cc3c25187876c82be7fb to your computer and use it in GitHub Desktop.
Per emailed question, utilizing the example in my blog post "User Dropdown List & Custom Notification Routing in Gravity Forms" for multiple fields that have the custom 'user-emails' CSS class to email multiple people
<?php
/**
* Change the sent to email address in the notification
*
* @author Joshua David Nelson, josh@joshuadnelson.com
**/
// Route to user address from drop down list, update the '1' to the ID of your form
add_filter( 'gform_notification_1', 'route_user_email_notification', 10, 3 );
function route_user_email_notification( $notification, $form , $entry ) {
// Create an array to store email addresses for notification
$emails = array();
// Go through each field to find the user-emails fields
foreach( $form['fields'] as &$field ) {
// Make sure this is the right field
if( $field['type'] != 'select' || strpos($field['cssClass'], 'user-emails') === false )
continue;
// Pull out the user id selected, by the field id and the $entry element
$field_id = (string) $field['id'];
$user_id = $entry[ $field_id ];
// set the email address to send the email to
if( !empty( $user_id ) ) {
$email_address = get_the_author_meta( 'user_email', $user_id );
// Just double checking the author meta is an email address
if( is_email( $email_address ) ) {
$emails[] = $email_address;
}
}
}
// If we have some emails in our array, turn it into a comma-separated list and update the notification
if ( ! empty( $emails ) ) {
$email_to = implode(', ', $emails );
$notification[ 'to' ] = $email_to;
}
return $notification;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment