You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需求:通过URL参数自动提交WordPress GravityForm表单

How to Auto-Submit Gravity Forms When URL Contains autosubmit=true

Got it, here's a straightforward solution to add that auto-submit functionality to your Gravity Forms. Since Gravity Forms doesn’t include this out of the box, we’ll use a combination of WordPress hooks and JavaScript to make it work smoothly.

Step 1: Enqueue Custom JavaScript in WordPress

First, add this PHP snippet to your theme’s functions.php file (or a custom plugin—plugins are safer for long-term changes). This ensures our script loads only when Gravity Forms is active.

add_action('wp_enqueue_scripts', 'gf_autosubmit_enqueue_script');
function gf_autosubmit_enqueue_script() {
    // Only load if Gravity Forms is installed and active
    if (class_exists('GFCommon')) {
        wp_enqueue_script(
            'gf-autosubmit',
            get_stylesheet_directory_uri() . '/js/gf-autosubmit.js', // Adjust this path to match your setup
            array('jquery'), // Depend on jQuery (Gravity Forms uses it too)
            '1.0',
            true // Load in the footer for better performance
        );
    }
}

Step 2: Create the Auto-Submit JavaScript File

Next, create a gf-autosubmit.js file in your theme’s js folder (make the folder if it doesn’t exist). This script will check for the autosubmit=true parameter, wait for the form to populate with URL data, then trigger submission.

jQuery(document).ready(function($) {
    // Helper function to grab URL parameters
    function getUrlParam(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        const regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        const results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    }

    // Check if autosubmit is enabled in the URL
    const autoSubmit = getUrlParam('autosubmit');
    if (autoSubmit.toLowerCase() === 'true') {
        // Wait for Gravity Forms to finish populating fields from the URL
        $(document).on('gform_post_render', function(event, formId) {
            const $form = $('#gform_' + formId);
            // Trigger form submission
            $form.submit();
        });

        // Fallback for AJAX-loaded forms (in case the event doesn’t fire immediately)
        setTimeout(function() {
            $('form.gform_form').each(function() {
                $(this).submit();
            });
        }, 1000); // Adjust this timeout if your form takes longer to load
    }
});

Important Notes:

  • Validation Rules Apply: The auto-submit will only work if all required fields are correctly populated via the URL. If any fields fail validation (like missing required data or invalid formats), the form won’t submit—just like a manual submission.
  • AJAX Forms: The gform_post_render event handles forms loaded via AJAX (a common Gravity Forms feature). The fallback timeout ensures we cover edge cases where the event might not trigger right away.
  • Path Adjustments: Double-check the path to your gf-autosubmit.js file in the PHP snippet. If you’re using a child theme, get_stylesheet_directory_uri() points to the child theme folder, which is correct.
  • Testing: Try a URL like https://your-site.com/form-page/?field_id=value&autosubmit=true to test—replace field_id with your actual form field ID and value with valid data.

If you run into issues, check your browser’s console for errors—common fixes include correcting file paths or ensuring all required URL parameters are present.

内容的提问来源于stack exchange,提问作者user9244498

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:41:03