Last active
August 1, 2016 21:52
-
-
Save joewils/1393b60c2b836ab202cc to your computer and use it in GitHub Desktop.
WordPress ShortCode for Embedding HubSpot Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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