With Gravity Forms, we can quickly set an email address in the admin panel, determining who gets notified when a new form entry is submitted. We can even set up multiple notifications with different content going to different recipients. This is all available from within the WordPress admin panel and requires no code to set up.
Sometimes, however, we need the notification to go to different email addresses, depending on certain information the user has filled out in the web form.
For example, I have recently worked on a site where visitors can get home improvement quotes from various contractors in their area. To get a quote, they have to fill out a form which requires their email address, description of the project for which they need a quote, etc.
The form also allows the user to select the businesses from which they are requesting a quote. In this case, the businesses themselves need to receive an email containing the form submission details, so setting an email address ahead of time in the admin panel will not suffice.
Since the form is set up using Gravity Forms, we first need to hook into the form notification process like so:
add_filter('gform_notification_2', 'hsr_quote_notification', 10, 3);
Here, we are basically telling Gravity Forms to run a function we will define, called hsr_quote_notification(), before sending off the notification email. This gives us a chance to touch the notification object and modify its parameters (in this case, we will add extra email addresses based on the businesses the user selected).
Note that in the filter above, we are referencing the ID of the form (in this case 2). We are also setting the filter priority (WordPress uses a default of 10), as well as telling the filter how many variables are going to be passed to our function (3). You can read more about the WordPress add_filter function here, and more in particular about the gform_notification filter here.
As far as writing our function to handle the notification, we note again that Gravity forms will pass three variables to us, which you can see in our function definition below. The $notification array is going to contain information about the email contents and destination. We will add some email addresses into the array and return the altered array for Gravity Forms to process. The only other variable we will need to utilize here is the $entry array, which will contain the user-submitted form data.
function hsr_quote_notification($notification, $form, $entry){ // Get the business(es) from which the user wants a quote // The form field containing the business info has an ID of 10 $biz = $entry[10]; // if we have more than one business, they will be separated by commas // so we need to turn this string into an array $bizzes = explode(',', $biz); // next, we'll loop through the array of businesses foreach($bizzes as $biz){ // trim extra whitespace and get post by business name // note that in our case, the post has a post type of 'business' $biz = get_page_by_title(trim($biz), '', 'business'); // get business email, which is stored in the post meta data // In particular, this is a custom field called 'email' $biz_email = get_post_meta($biz->ID, 'email', true); // append email address to the existing string for the email destination $notification['to'] .= ', ' . $biz_email; } // end foreach: businesses // finally, we need to return our altered $notification array return $notification; }
As with most things when developing with WordPress, this problem boils down to finding the right process to hook into, knowing what data is being placed in our hands, and being able to alter that data to better serve our needs.
Leave a Reply