Gravity Forms Filter Example

Posted by on Apr 29, 2017 in Technology, Web Development, WordPress

RocketGenius’s Gravity Forms plugin for WordPress is arguably one of the best form builder plugins around. Support is great, licensing is simple, and the tool itself is exemplary. It’s GUI form builder is second to none, and even allows management of contact dropdowns, so notifications can be sent dynamically to the right person based on user choice (e.g. regional sales guy, department head, etc).

However, managing a large number of contact destinations with the GUI can be problematic for deployment. Luckily, the same thing can be done with some simple coding. By modifying the active theme’s functions.php file (another topic) we can add a Gravity Forms filter to address this.

Below is an example, with actual email addresses replaced, from a project I worked on recently.

// Gravity Forms location list population, update the '1' to the ID of the form
add_filter( 'gform_pre_render_1', 'populate_recipient_email_list' );
function populate_recipient_email_list( $form ){
	foreach( $form['fields'] as &$field ) {
	// If field isn't a dropdown and not our class, keep going
	// Filter arguments until we find the one we want
        if( $field['type'] !== 'select' || strpos($field['cssClass'], 'locationSelector') === false )
            continue;
		// The first, "select" option
		$choices = array( array( 'text' => 'Select', 'value' => '' ) );

		$choices[] = array( 'text' => 'USA', 'value' => '' );
		$choices[] = array( 'text' => '---------------', 'value' => '' );
		$choices[] = array( 'text' => 'AL', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'AK', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'AZ', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'AR', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'CA (Northern)', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'CA (Southern)', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'CO', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'CT', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'DE', 'value' => 'test@test.com' );

		... (removed for brevity)

		$choices[] = array( 'text' => 'WI', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'WY', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => '---------------', 'value' => '' );
		$choices[] = array( 'text' => 'Canada', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'Carribean', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'Mexico', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => 'S. America', 'value' => 'test@test.com' );
		$choices[] = array( 'text' => '---------------', 'value' => '' );
		$choices[] = array( 'text' => 'Other', 'value' => 'test@test.com' );

		$field['choices'] = $choices;
    }
    return $form;
}

// Route to location's email address from drop down list, update the '1' to the ID of the form
add_filter( 'gform_notification_1', 'route_contact_email_notification', 10, 3 );
function route_contact_email_notification( $notification, $form , $entry ) {
	foreach( $form['fields'] as &$field ) {
		// Find the right field
		if( $field['type'] != 'select' || strpos($field['cssClass'], 'locationSelector') === false )
			continue;
		// Pull out the location's email address selected, by the field location and the $entry element
		$field_id = (string) $field['id'];
		$email_to = $entry[ $field_id ];
	}
	if ( !empty( $email_to ) && is_email( $email_to ) ) {
		$notification[ 'to' ] = $email_to;
	}
	return $notification;
}