Skip to content

Instantly share code, notes, and snippets.

@joewils
Last active August 1, 2016 21:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joewils/1393b60c2b836ab202cc to your computer and use it in GitHub Desktop.
Save joewils/1393b60c2b836ab202cc 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