Skip to content

Instantly share code, notes, and snippets.

@derekcavaliero
derekcavaliero / hubspot-form-a11y-polyfill.js
Last active June 16, 2023 13:34
HubSpot Embedded Form Accessibility Polyfills
/**
* HubSpot Embedded Form Accessibility Pollyfills
*
* This script fixes the HubSpot HTML blunders that make their embedded forms inaccessible for assistive technology.
* - Replaces/removes improper use of <fieldset>, <legend>, <label>, and role attributes.
* - Note - this can cause forms configured for mulitple column field layouts to break - you will need to adjust your CSS accordingly.
**/
hubspotFormA11y = {
@derekcavaliero
derekcavaliero / hubspot-embedded-video-gtm-datalayer-adapter.js
Last active June 21, 2023 10:44
HubSpot - Embedded Video GTM dataLayer Adapter
/**
* You'll want to add this to GTM using an "All Pages" trigger -
* Note - make sure you set it to only fire "once per-page" to prevent issues with re-registering the listener on SPAs
**/
window.addEventListener('message', function(message){
if (message.origin != 'https://play.hubspotvideo.com') return;
/*
@derekcavaliero
derekcavaliero / gforms-hidden-field-class-markup.php
Created December 10, 2021 21:30
Gravity Forms - Add CSS Class to Hidden Input Using Input Name
<?php
add_filter( 'gform_field_input', function ( $input, $field, $value, $lead_id, $form_id ) {
if ( $field->inputName !== '' && $field->type == 'hidden' ) {
$input = "<input type='hidden' name='input_{$field->id}' class='{$field->inputName}' value='{$value}'>";
}
return $input;
}, 10, 5 );
@derekcavaliero
derekcavaliero / pardot-form-submission-postmessage-event
Last active April 1, 2021 17:02
Push a window.postMessage event to the parent window for iframed Pardot forms. You can then intercept this message via Google Tag Manager (see: X) for an example.
<script>
if ( window.parent.postMessage ) {
window.parent.postMessage({
'event': 'Form Submission', // Never change this
'form_id': '1234', // The Pardot form ID (you can get this from the form edit page URL)
'form_label': '[Whitepaper] - The 2020 State of Digital Accessibility', // This can really be whatever you want.. but make sure its descriptive
'form_action': 'Gated Resource Download' // See suggested Form Actions
}, '*');
}
</script>
@derekcavaliero
derekcavaliero / hubspot-global-form-submit-event-example
Created July 24, 2020 15:09
HubSpot - Global Form Submit Event Example
<script>
(function() {
var dataArrayToObject = function(data) {
var fields = {};
for ( var i = 0; i < data.length; i++ ) {
fields[data[i].name] = data[i].value;
}
@derekcavaliero
derekcavaliero / hubspot-form-map-fields-to-query-string
Last active September 23, 2021 23:47
HubSpot Form - Dynamic Redirect URL from Submitted Data
hbspt.forms.create({
portalId: "XXXXXX",
formId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
inlineMessage: 'Please wait...',
onFormSubmit: function($form){
// We'll store our "object" to transform into a query string here.
// If you want to hard-code a value that isn't part of the form - add it in here.
var queryObj = {
q3: 'myself'
/* !!!!!!
ALWAYS CREATE A BACKUP BEFORE RUNNING THESE COMMANDS!
!!!!!! */
UPDATE wp_options
SET option_value = replace(option_value, 'https://old-domain.com', 'https://new-domain.com')
WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts
SET guid = replace(guid, 'https://old-domain.com', 'https://new-domain.com');
@derekcavaliero
derekcavaliero / wordpress-pardot-iframe-src-filter.php
Created September 13, 2017 18:39
WordPress Filter - Append permalink to Pardot iframe src attribute
<?php
/**
* Finds all Pardot form iframes and appends a ?embedded_on= query parameter to the source.
* Very useful for fixing the crappy issues with pardot tracking...
*/
add_filter( 'the_content', 'pardot_iframe_src_filter', 99, 1 );
function pardot_iframe_src_filter( $content ) {
global $post;