Skip to content

Instantly share code, notes, and snippets.

Embed
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.