Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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