ResoundingEchoes

The Volume Just Increases

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter
  • YouTube
  • Home
  • Portfolio
  • Blog
  • Interact
    • Say Hello
    • Request A Quote
    • Join Our Newsletter
You are here: Home / Development / Gravity Forms: Add Extra Email Addresses To Notifications

Gravity Forms: Add Extra Email Addresses To Notifications

February 5, 2015 by Michael Hull Leave a Comment

gravityforms_logo_500_2

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.

Filed Under: Development

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

How can we help you?   »

Recent Posts

  • Creating An xy-Plane Component With Vue.js
  • Set Theory And Russell’s Paradox
  • Function And Set Notation
  • What Is A Function
  • My First Peek At Angular 2

Comments From The Blog

  • Eric Celeste on Pre-populating Checkboxes in Gravity FormsI'm not sure why the "gform_field_value" hook didn't do the trick for you. I use this to pre-check checkboxes myself.
  • gpence on Creating A Clock With HTML, CSS, and JavaScriptWhy does your example clock have a zero at the top instead of the roman numeral for 12?
  • Mej Oro on How To Style Draggable Elements To Indicate DraggabilityHi, thank you for having written this article ...but really I totally was not able to make it works: I
  • Jonathan Daggerhart on Function And Set NotationGreat post! This is very enlightening to someone like me who has a self-taught programming background and practically no background

© 2025 ResoundingEchoes