gform_enqueue_scripts

Description

This filter is executed during the process of enqueuing scripts for each form in the current page. When developing custom field types that require extra scripts, this hook should be used to enqueue the custom scripts when appropriate.

Usage

add_action( 'gform_enqueue_scripts', 'your_function_name', 10, 2 );

You can also specify this per form by adding the form id after the hook name.

add_action( 'gform_enqueue_scripts_6', 'your_function_name', 10, 2 );

Parameters

  • $form Form Object

    The current form object.

  • $is_ajax bool

    Specify if the form is configured to be submitted via AJAX.

Examples

1. Enqueue custom script

This example enqueues a custom script for all AJAX forms.

add_action( 'gform_enqueue_scripts', 'enqueue_custom_script', 10, 2 );
function enqueue_custom_script( $form, $is_ajax ) {
if ( $is_ajax ) {
wp_enqueue_script( 'custom_script', 'path/file.js' );
}
}

2. Dequeue stylesheets

This example shows how you can dequeue the stylesheets for a specific form.

add_action( 'gform_enqueue_scripts_1', 'dequeue_gf_stylesheets', 11 );
function dequeue_gf_stylesheets() {
wp_dequeue_style( 'gforms_reset_css' );
wp_dequeue_style( 'gforms_datepicker_css' );
wp_dequeue_style( 'gforms_formsmain_css' );
wp_dequeue_style( 'gforms_ready_class_css' );
wp_dequeue_style( 'gforms_browsers_css' );
}

3. Manually include RTL stylesheet

Gravity Forms will automatically include the RTL stylesheet if WordPress indicates an RTL language is in use. However, if you haven’t set the language yet and want to test with the RTL styles, you can use the following to include the stylesheet.

add_action( 'gform_enqueue_scripts', 'enqueue_custom_script' );
function enqueue_custom_script() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
wp_enqueue_style( 'gforms_rtl_css', GFCommon::get_base_url() . "/css/rtl{$min}.css", null, GFCommon::$version );
}

4. Dequeue script

This example shows how you can dequeue a form script for a specific form.

add_action( 'gform_enqueue_scripts_1', function() {
wp_dequeue_script( 'gform_placeholder' );
}, 11 );

Placement

This code should be placed in the header.php file of your active theme just before the wp_head() function call or a custom functions plugin.

Source Code

This filter is located in GFFormDisplay::enqueue_form_scripts() in form_display.php.