Skip to content

Instantly share code, notes, and snippets.

@banarsiamin
Last active February 1, 2024 15:11
Show Gist options
  • Save banarsiamin/e78a7c599c32a28ec3ceb1f040e08b4e to your computer and use it in GitHub Desktop.
Save banarsiamin/e78a7c599c32a28ec3ceb1f040e08b4e to your computer and use it in GitHub Desktop.
Elemntor for with Hubspot leade insert in wp php
<?php
add_action( 'elementor_pro/forms/new_record', function( $record, $ajax_handler ) {
$form_name = $record->get_form_settings( 'form_name' );
$data = aytHubspotForm();
$ajax_handler->add_response_data( 'hubspot_data', $data );
}, 10, 2);
function aytHubspotForm($data=array()){
//echo"<PRE>";print_r($_REQUEST);die;
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$IP = $_SERVER['HTTP_CLIENT_IP'];
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {$IP = $_SERVER['REMOTE_ADDR']; }
$pageUrl = isset($_REQUEST['referrer'])?$_REQUEST['referrer']:get_site_url();
$referer_title = isset($_REQUEST['referer_title'])?$_REQUEST['referer_title']:'Api Page';
$hubSpotformId= 'dab76538-8c05-4ceb-b9bd-8ab78639c3df';
$_aytDATA = isset($_REQUEST['form_fields'])?$_REQUEST['form_fields']:'';
$form_id = isset($_REQUEST['form_id'])?$_REQUEST['form_id']:'';
$hs_context = array('hutk'=> 'hubspotutk','ipAddress' => $IP,'pageUrl'=> $pageUrl,'pageName'=> $referer_title);
$hs_context_json = json_encode($hs_context);
if($form_id=='303c3a1'){
/* 30 Minute Consultation Form HS */
$save=true;
$hubSpotformId= 'dab76538-8c05-4ceb-b9bd-8ab78639c3df';
$firstname = isset($_aytDATA['name'])?$_aytDATA['name']:'';
$email = isset($_aytDATA['email'])?$_aytDATA['email']:'newinquery@yopmail.com';
$str_post = "firstname=" . urlencode($firstname)
. "&email=" . urlencode($email)
. "&hs_context=" . urlencode($hs_context_json);
}elseif($form_id=='303c3a1'){
/* 30 Minute Consultation Form HS */
$save=true;
$hubSpotformId= 'dab76538-8c05-4ceb-b9bd-8ab78639c3df';
$firstname = isset($_aytDATA['name'])?$_aytDATA['name']:'';
$email = isset($_aytDATA['email'])?$_aytDATA['email']:'newinquery@yopmail.com';
$str_post = "firstname=" . urlencode($firstname)
. "&email=" . urlencode($email)
. "&hs_context=" . urlencode($hs_context_json);
}else{$save=false;}
if($save==true){
$endpoint = "https://forms.hubspot.com/uploads/form/v2/26454388/$hubSpotformId";
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
@curl_setopt($ch, CURLOPT_URL, $endpoint);
@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = @curl_exec($ch); //Log the response from HubSpot as needed.
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
@curl_close($ch);
return json_encode($response,true);
}
return false;
}
?>
Embeded code
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
hbspt.forms.create({
region: "na1",
portalId: "23878667",
formId: "e5351343-3a6a-453b-9ed2-a26f52ef8b6b"
});
</script>
@banarsiamin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment