Skip to content

Instantly share code, notes, and snippets.

@lukecav
Forked from joewils/hubspot_form
Created August 1, 2016 21:52
Show Gist options
  • Save lukecav/d4d88ff07006fbb2a84eae2456e35924 to your computer and use it in GitHub Desktop.
Save lukecav/d4d88ff07006fbb2a84eae2456e35924 to your computer and use it in GitHub Desktop.
WordPress ShortCode for Embedding HubSpot Forms
<?php
// Usage: [hubspot_form form_id="1234-5678-90123-45676"]
// Inspiration: https://rschu.me/an-easy-way-to-embed-hubspot-forms-in-wordpress/
// API Documentation: http://developers.hubspot.com/docs/methods/forms/advanced_form_options
add_shortcode('hubspot_form', function($atts) {
extract(shortcode_atts(array(
'form_id' => null,
'redirect_url' => null
), $atts));
if( !is_null($form_id) ) {
$post_id = get_the_ID();
$hubspot_form = array(
'portalId' => '1234567',
'formId' => $form_id,
'target' => '#joecode-'.$post_id,
'inlineMessage' => 'Thank you for contacting us, we will be in touch.'
);
if( !is_null($redirect_url) ) {
$hubspot_form['redirectUrl'] = $redirect_url;
}
$html_form = '<div id="joecode-'.$post_id.'"></div>';
$html_form .= '<script>hbspt.forms.create( ' . json_encode( $hubspot_form ) . ' );</script>';
return $html_form;
}
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment