Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Tommy-b/ddd5f0406b5afcdad2712721e5960f32 to your computer and use it in GitHub Desktop.
Save Tommy-b/ddd5f0406b5afcdad2712721e5960f32 to your computer and use it in GitHub Desktop.
This allows your gravity forms to load via ajax to ensure they work correctly with page transitions that have no window/document.onload
<?php
// ***IMPORTANT*** PLACE THIS PART IN YOUR ENQUEUE SCRIPTS FUNCTION AFTER YOUR SCRIPTS.JS ENQUEUE
wp_localize_script( 'change-me-to-your-script-file-handle', 'ajax', array(
'url' => admin_url( 'admin-ajax.php' )
));
// You can now use ajax.url as the URL in your ajax calls
// PLACE THIS ANYWHERE IN YOUR FUNCTIONS.PHP FILE
// Add the action and nopriv action to account for users not logged in
add_action( 'wp_ajax_nopriv_load_gravity_form', 'load_gravity_form' );
add_action( 'wp_ajax_load_gravity_form', 'load_gravity_form' );
function load_gravity_form() {
// These return the following:
// The form ID
$gfId = intval($_GET['formid']);
// Rather or not to add the form title - converted to a boolean
$gfTitle = filter_var($_GET['formtitle'], FILTER_VALIDATE_BOOLEAN);
// Rather or not to add the form description - converted to a boolean
$gfDesc = filter_var($_GET['formdescr'], FILTER_VALIDATE_BOOLEAN);
// Rather or not to the form should be submitted via ajax - converted to a boolean
$gfAjax = filter_var($_GET['formajax'], FILTER_VALIDATE_BOOLEAN);
// This is the shortcode to embed the form
gravity_form($gfId, $gfTitle, $gfDesc, false, '', $gfAjax, 1);
die();
}
?>
This allows your gravity forms to load via ajax to ensure they work correctly with page transitions that have no window/document.onload
<?php
// This function will enqueue the necessary styles and scripts for the
// specified Gravity Form. This is useful when manually embedding a form
// outside the WordPress loop using the function call.
//
// This is placed in a foreach loop to account for all forms including
// future forms you may not have an ID for yet. If you only have a couple
// forms and know the Ids won't change you can just add them as such:
// gravity_form_enqueue_scripts( 1, true );
//
// Place this right before wp_head();
$forms = GFAPI::get_forms();
foreach ($forms as $form) {
gravity_form_enqueue_scripts( $form['id'], true );
}
?>
<div class= "form-wrap">
<!-- I am using Gravity forms ACF field to allow user to select which form they wish to display.
You can download and find more at https://github.com/DannyvanHolten/acf-gravityforms-add-on -->
<?php $acf_field_form_id = get_sub_field( 'field_name_for_form' ); ?>
<!-- Set the form ID to match user selection, set whether the form should show
title and/or descrition, and rather it should be submitted via ajax or not -->
<div class="gfid" data-id="<?php echo $acf_field_form_id; ?>" data-title="true" data-descr="true" data-ajax="true"></div>
</div>
// Grab all items from your page template file
var yourForm = $('.form-wrap'),
gfItem = yourForm.find('.gfid'),
gfNum = gfItem.attr('data-id'),
gfId = parseInt(gfNum),
gfTitle = gfItem.attr('data-title'),
gfDesc = gfItem.attr('data-descr'),
gfAjax = gfItem.attr('data-ajax');
if (yourForm.length) {
$.ajax({
type: "GET",
// Make sure this matches what you set in your wp_enqueue_script
url: ajax.url,
data: {
// The name of the function declared in functions.php
action: 'load_gravity_form',
// The data pulled from page-template.php
formid: gfId,
formtitle: gfTitle,
formdescr: gfDesc,
formajax: gfAjax,
}
}).done(function (data) {
yourForm.html(data);
});
}
@atoupet-toki
Copy link

Wow, thanks for all these snippets! Did you manage to make it work with a recaptcha in the form?

@Striffly
Copy link

Striffly commented Apr 23, 2024

@atoupet-toki with the updated version of the Gravity Forms reCAPTCHA Add-On, reCAPTCHA scripts are now automatically enqueued.

BTW, if you're interested, I've created an "updated" version of this script with functional conditional logic : https://gist.github.com/Striffly/fa79b85901dd6c98a8075a35d7dc45ee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment