1

For adding defer to jquery link, I would like to move the inline jQuery script of Ajax Gravity Form to the footer.

How can I do for this?

enter image description here

Sukhi
  • 13,261
  • 7
  • 36
  • 53
forevereffort
  • 432
  • 1
  • 3
  • 14

2 Answers2

3

This worked for me. I hope this will work for you also.

// Force Gravity Forms to init scripts in the footer and ensure that the DOM is loaded before scripts are executed
add_filter( 'gform_init_scripts_footer', '__return_true' );
add_filter( 'gform_cdata_open', 'wrap_gform_cdata_open', 1 );
function wrap_gform_cdata_open( $content = ” ) {
    if ( ( defined('DOING_AJAX') && DOING_AJAX ) || isset( $_POST['gform_ajax'] ) ) {
        return $content;
    }
    $content = 'document.addEventListener( "DOMContentLoaded", function() { ';
    return $content;
}
add_filter( 'gform_cdata_close', 'wrap_gform_cdata_close', 99 );
function wrap_gform_cdata_close( $content = '' ) {
    if ( ( defined('DOING_AJAX') && DOING_AJAX ) || isset( $_POST['gform_ajax'] ) ) {
        return $content;
    }
    $content = ' }, false );';
    return $content;
}
Tanmoy
  • 41
  • 7
0

This might work:

/**
 * Force GFORM Scripts inline next to Form Output
 *
 * force the script tags inline next to the form. This allows
 * us to regex them out each time the form is rendered.
 * 
 * see strip_inline_gform_scripts() function below
 * which implements the required regex
 */
add_filter("gform_init_scripts_footer", "__return_false");

/**
 * Strip out GForm Script tags
 *
 * note: this diables post and pre render hooks which are triggered
 * when the form renders so if you need these then it's important
 * to manually re-add them in your compiled JS source code
 */

add_filter("gform_get_form_filter", function( $form_string, $form ) {
  return $form_string = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $form_string);
}, 10, 2);
mikerojas
  • 2,238
  • 1
  • 4
  • 7