Mar 122014
 
Gravity

I am currently developing a WordPress website for a genealogical organisation. One of the things they wanted was for any website visitor to be able to complete a contact form and for the content of the form to go direct to their member and not to the WordPress Admin email address.

The first thing to do was to create the contact form in Gravity Forms. This was just a simple form requiring first and last names, email address and message text. To this form I added a hidden field to store the email address that the form should be submitted to. I also created a custom notification to go along with this form. I called the notification ‘Profile Contact’. It does not matter which of the ‘Send To’ options you choose in this notification.

The next thing was a bit more complicated and it caused me to have an email exchange with Chris and Richard at Gravity forms support to get this up and running.

The page that accesses this contact form is based on a customised page template. This is the relevant piece of code that calls the contact form

<div class="cs_detail">		
<div class="cs_detail_header">Contact Details:</div><!--/cs_detail_header-->
<div class="cs_detail_content">
For more information please contact: 
<?php
$_SESSION['member'] = $study_member;
$contact = $wpdb->get_row("SELECT * FROM members WHERE number = '$study_member'");
$details = $contact->title." ".$contact->firstname." ".$contact->surname;								
echo " <b>". $details."</b>  ";
?>
<button class="eModal-1">Send Email</button>
</div><!--/cs_detail_content-->
</div><!--/cs_detail-->

This code needs a little explanation, the session variable is used to pass the result of another query on another custom table over to the custom Gravity Forms function.  The button is provided by the Easy Modal plugin that I use to display the contact form as a modal window over the page.

Two functions need to be added to the Themes functions.php file.

The first one is:

/** Set the email to address on the contact form **/
add_filter('gform_notification', 'change_autoresponder_email', 10, 3);
function change_autoresponder_email( $notification, $form, $entry ) {

    // There is no concept of user notifications anymore, so 
    // we will need to target notifications based on other 
    // criteria, such as name
    if($notification["name"] == "Profile Contact"){

        // toType can be routing or email
        $notification['toType'] = "email"; 
        // change the "to" email address
        $notification['to'] = $entry["4"];

    }

    return $notification;
}

The code is copied straight from the Gravity Forms documentation here. I used the code in Example 2 and just changed the value of the $entry to 4 as this was the field number of the  hidden field. I also changed the $notification to reflect the name of my custom notification form. (Don’t forget to delete the default notification or you will still get emails to the WordPress Admin account too).

The code for the second custom function is

// will populate a field in your form which has a parameter name of 'email_to'
add_filter("gform_field_value_email_to", "populate_email_address");
function populate_email_address($value){
	global $wpdb;
	$study_member = $_SESSION['member'];
	$contact = $wpdb->get_row("SELECT * FROM members WHERE number = '$study_member'");
	$email = $contact->studyalias1."@mydomain.org";
	return $email;
}

There is some explanation of this code required. As I am connecting to the WordPress database to get the email address from a custom table the global $wpdb variable needs to be added to the code. The membership number variable ($study_member) is populated from the session variable and then used in the query. The email address is constructed and then passed back to the Gravity Form in the hidden field. To prove this you can put it in a visible field on the form for testing purposes.

This works exactly as required by my organisation and shows the versatility of Gravity Forms.

Other useful links relevant to this post

Easy Modal plug-in.

Gravity Forms plug-in.

Gravity Forms documentation on the entry object.

 Posted by at 5:08 pm