Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravity Forms/PHP: how to integrate gform_post_submission hook? WordPress

  • SOLVED

I'm using the wordpress plugin "Gravity Forms" for a newsletter signup form on my website.
Form fields are: E-Mail Address, First Name, Last Name.

The submitted form data needs to be passed to a third party PHP Soap API Gateway.
I already have a PHP Script to pass data to the third party API Gateway. The script is working fine if you use it as a standalone script and populate the data fields manually.

<?php
/*******************
* PHP Soap API Gateway
* before any of the calls can be executed, you need to call initSession once
* see WSDL file for more information
*******************/

####################################################
# api settings
####################################################

echo "<pre>";
$wsdl_url="###WSDL URL###"; // wsdl url
$apiKey = "###YOUR API KEY###"; // api key, to authenticate
$InsertIntoList = "###YOUR LIST ID###"; // list id
$form_id = "###YOUR FORM ID###"; // form id

$soap = new SoapClient($wsdl_url);

####################################################
# insert new user
####################################################

$newReceiver = array (
'email' => '[email protected]', // required, while everything else is optional
'registered' => time(), // unix timestamp, date of registration [optional]
'source' => 'API', // helps you identifing where the user cames from [optional]
'firstname' => 'John', // firstname [optional]
'lastname' => 'Doe', // lastname [optional]
);

$return = $soap->add($apiKey, $InsertIntoList, $newReceiver);
if($return->status=="SUCCESS"){ // successfull creation
$soap->sendActivationMail($apiKey, $form_id, $newReceiver['email']); // send double-opt-in activation email
}else{ // call failed
unset($newReceiver['registered']); // delete date of registration
$return = $soap->update($apiKey, $InsertIntoList, $newReceiver);
}
?>


Gravity Forms offers the gform_post_submission hook which runs after entry data has been saved and has access to the submitted form data/database entries.
The hook looks like this:
<?php
add_action("gform_post_submission_6", "post_submission", 10, 2);
?>


A short hook documentation and an example:
https://s3.amazonaws.com/gravityforms/documentation/gform_post_submission.pdf
http://www.gravityhelp.com/forums/topic/presale-questions-hooksapi-add-ons-and-multi-lingual-forms

Some more examples of how this hook can be used:
http://pastie.org/pastes/1330590
http://pastie.org/pastes/1288363
http://pastie.org/pastes/1288363
http://pastie.org/pastes/1080289
http://pastie.org/pastes/1072792
http://pastie.org/pastes/1057774
http://pastie.org/pastes/968792
http://pastie.org/pastes/940498

<strong>I need you to make this work:</strong>
1. User submits data with the form. --> working
2. Gravity Forms saves form data. --> working
3. Some PHP hook magic gathers form data, matches it to the api field names and passes it --> ???
4. to the API code which transfers it to the third party. --> ???

The whole code is going to be saved into the themes functions.php.
<strong>
Thank You!</strong>

Answers (3)

2011-02-17

John Cotton answers:

It would be helpful to have a link to the form page - without it, I'm guessing at your field ids.

But something like this will work:

add_action("gform_post_submission_2", "post_submission", 10, 2);

function post_submission($lead, $form) {
$wsdl_url="###WSDL URL###"; // wsdl url

$apiKey = "###YOUR API KEY###"; // api key, to authenticate

$InsertIntoList = "###YOUR LIST ID###"; // list id

$form_id = "###YOUR FORM ID###"; // form id



$soap = new SoapClient($wsdl_url);
$newReceiver = array (
'email' => $lead['1'], // required, while everything else is optional
'registered' => time(), // unix timestamp, date of registration [optional]
'source' => 'API', // helps you identifing where the user cames from [optional]
'firstname' => $lead['2'], // firstname [optional]
'lastname' => '$lead['3'], // lastname [optional]
);


$return = $soap->add($apiKey, $InsertIntoList, $newReceiver);

if($return->status=="SUCCESS"){ // successfull creation

$soap->sendActivationMail($apiKey, $form_id, $newReceiver['email']); // send double-opt-in activation email

}else{ // call failed

unset($newReceiver['registered']); // delete date of registration

$return = $soap->update($apiKey, $InsertIntoList, $newReceiver);

}

}


The $lead values can be figured out from looking at the form on the page. So, for example, if the text box that you type your email into has a name attribute of "input_7", then you would use $lead['7'] to access its value in the function above.


John Cotton comments:

Hey Oleg - I've add the SOAP variables! I'll split the prize :)


askingyou comments:

Hey John,

after deleting the single quotation mark before
'$lead['3'],
your code works.

Thank You!


John Cotton comments:

<blockquote>after deleting the single quotation mark</blockquote>

Sorry, yea, that shouldn't have been there....that will teach me to type straight in...:)

2011-02-17

Oleg Butuzov answers:


add_action("gform_post_submission_6", "post_submission_func", 10, 2);
function post_submission_func(){
$wsdl_url="###WSDL URL###"; // wsdl url
$apiKey = "###YOUR API KEY###"; // api key, to authenticate
$InsertIntoList = "###YOUR LIST ID###"; // list id
$form_id = "###YOUR FORM ID###"; // form id

$soap = new SoapClient($wsdl_url);

####################################################
# insert new user
####################################################

$newReceiver = array (
'email' => '[email protected]', // required, while everything else is optional
'registered' => time(), // unix timestamp, date of registration [optional]
'source' => 'API', // helps you identifing where the user cames from [optional]
'firstname' => 'John', // firstname [optional]
'lastname' => 'Doe', // lastname [optional]
);

$return = $soap->add($apiKey, $InsertIntoList, $newReceiver);
if($return->status=="SUCCESS"){ // successfull creation
$soap->sendActivationMail($apiKey, $form_id, $newReceiver['email']); // send double-opt-in activation email
}else{ // call failed
unset($newReceiver['registered']); // delete date of registration
$return = $soap->update($apiKey, $InsertIntoList, $newReceiver);
}
return $return;
}


Oleg Butuzov comments:

instead '[email protected]', you can past the lead array element that fit your form.

i think i lose Jhon just few seconds. but still i lose. he win.


Oleg Butuzov comments:

oops, no i am not...

John, you forgot to add Soap initiation variables in your hook function.


askingyou comments:

Hey Oleg,

after adding ($entry, $form) in line 2
function post_submission_func($entry, $form)
and following your instructions concerning the array elements your code works, too.

Thank You!

2011-02-17

spivurno answers:

I've notated which values will need to be updated in the code. The form ID is easy to spot in the Gravity Forms admin. Field IDs are a little more difficult to find but if you view the source of your form you should see something like this "input_1_4" for the IDs for fields. The "1" in this example is the form ID, and the "4" is the field ID.

<?php

// update the "1" in "gform_post_submission_1" to the ID of your Gravity Form
add_action("gform_post_submission_1", "post_submission_to_api");
function post_submission_to_api($entry){

// api settings

$wsdl_url = "###WSDL URL###"; // wsdl url
$apiKey = "###YOUR API KEY###"; // api key, to authenticate
$InsertIntoList = "###YOUR LIST ID###"; // list id
$form_id = "###YOUR FORM ID###"; // form id

$soap = new SoapClient($wsdl_url);

// insert new user

$newReceiver = array (

'email' => $entry[1], // UPDATE: 1 to the ID of your GF email field
'registered' => time(),
'source' => 'API-GFSubmission',
'firstname' => $entry[2], // UPDATE: 2 to the ID of your GF firstname field
'lastname' => $entry[3], // UPDATE: 3 to the ID of your GF lastname field

);

$return = $soap->add($apiKey, $InsertIntoList, $newReceiver);

if($return->status=="SUCCESS"){ // successfull creation
$soap->sendActivationMail($apiKey, $form_id, $newReceiver['email']); // send double-opt-in activation email
} else { // call failed
unset($newReceiver['registered']); // delete date of registration
$return = $soap->update($apiKey, $InsertIntoList, $newReceiver);
}

?>


askingyou comments:

Hey Spivurno,

after adding a closing curly bracket to the last line, which was opened after function post_submission_to_api($entry){ your code works, too.

Thank You!