Skip to content

Instantly share code, notes, and snippets.

@cheadrian
Last active January 18, 2024 10:33
Show Gist options
  • Save cheadrian/f3451671acb1c2c6f1afdd3a52043e6f to your computer and use it in GitHub Desktop.
Save cheadrian/f3451671acb1c2c6f1afdd3a52043e6f to your computer and use it in GitHub Desktop.
Webhook for Google Ads Lead Form extension using WordPress and WP Webhooks plugin and send data to email
/*
* Forward data received by webhook from Google Ads Form Extension
* to selected email
* Details at:
* https://che-adrian.medium.com/create-a-webhook-for-google-ads-lead-form-extension-using-wordpress-and-wp-webhooks-plugin-and-cd2a3c7fb1c3
*/
add_filter( 'wpwhpro/run/actions/custom_action/return_args', 'wpwh_ads_email', 10, 3 );
function wpwh_ads_email( $return_args, $identifier, $response_body ){
//Email where you receive the data
$to = "example@example.com";
$client_name = "";
$form_data = $response_body["content"]->user_column_data;
$message = $form_data;
/*
* If you need more security, you can handle the content of the google_key, e.g.:
* $expected_google_key = 'YOUR_RANDOM_KEY_FOR_FUNCTIONS.PHP_CODE';
* if ($response_body["google_key"] !== $expected_google_key) {
* // Trigger an error if the key doesn't match
* error_log('Invalid Google Key');
* return $return_args;
* }
*/
ob_start();
echo "<html><style>table, th, td { border: 1px solid black; width: 350px;}</style><table>";
foreach($form_data as $field){
echo "<tr>";
echo "<td>".$field->column_name."</td><td>".$field->string_value."</td>";
//Set client name inside variable to use at email subject
if($field->column_id == "FULL_NAME" || $field->column_id == "FIRST_NAME"){
$client_name = $field->string_value;
}
if($field->column_id == "LAST_NAME"){
$client_name .= " ".$field->string_value;
}
echo "</tr>";
}
echo "</table></html>";
$message = ob_get_clean();
/*
* Email subject will have one of the client name or the full name;
* That's depends on the Google Form type
*/
//You can edit "Google Lead Form"
$subject = $client_name." - Google Lead Form";
/*
* Adjust glead@example.com to your "from" email
* If that email doesn't exist on your server, it might send form data in spam or not at all
* You can edit "Google Lead Form"
*/
$headers = array(
"Content-Type: text/html; charset=UTF-8",
"From: Sender Name - Google Lead Form <glead@example.com>"
);
wp_mail( $to, $subject, $message, $headers );
return $return_args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment