Using Gravity Forms ‘gform_post_enqueue_scripts’ PHP action

The gform_post_enqueue_scripts action hook is executed at the end of the process of enqueuing scripts and styles for each form embedded in the current page by shortcode or block.

Usage

add_action('gform_post_enqueue_scripts', 'your_function_name', 10, 3);

Parameters

  • $found_forms (array) – An array of found forms using the form ID as the key to the ajax status.
  • $found_blocks (array) – A filtered version of the array returned by parse_blocks containing only Gravity Forms blocks.
  • $post (WP_Post) – The page or post which was processed.

More information

See Gravity Forms Docs: gform_post_enqueue_scripts

This hook was added in version 2.4.18.

Source code location: GFFormDisplay::enqueue_scripts() in form_display.php.

Examples

Basic Usage

Execute a function for each form found on the page:

add_action('gform_post_enqueue_scripts', function ($found_forms, $found_blocks, $post) {
    foreach ($found_forms as $form_id => $ajax_status) {
        // Do something for each form.
    }
}, 10, 3);

Add Custom CSS

Add custom CSS for each form:

function custom_form_css($found_forms, $found_blocks, $post) {
    foreach ($found_forms as $form_id => $ajax_status) {
        wp_enqueue_style('custom-form-css', '/path/to/your/css/file.css', array(), '1.0.0');
    }
}
add_action('gform_post_enqueue_scripts', 'custom_form_css', 10, 3);

Add Custom JavaScript

Add custom JavaScript for each form:

function custom_form_js($found_forms, $found_blocks, $post) {
    foreach ($found_forms as $form_id => $ajax_status) {
        wp_enqueue_script('custom-form-js', '/path/to/your/js/file.js', array('jquery'), '1.0.0', true);
    }
}
add_action('gform_post_enqueue_scripts', 'custom_form_js', 10, 3);

Load Conditional Script

Load a script conditionally for a specific form:

function conditional_form_script($found_forms, $found_blocks, $post) {
    if (array_key_exists(5, $found_forms)) { // Check if form with ID 5 exists on the page
        wp_enqueue_script('conditional-form-script', '/path/to/your/js/file.js', array('jquery'), '1.0.0', true);
    }
}
add_action('gform_post_enqueue_scripts', 'conditional_form_script', 10, 3);

Debug Found Forms

Print debug information about the found forms:

function debug_found_forms($found_forms, $found_blocks, $post) {
    error_log('Found forms: ' . print_r($found_forms, true));
}
add_action('gform_post_enqueue_scripts', 'debug_found_forms', 10, 3);