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

Gravity Forms前端重复字段组/板块功能实现求助

How to Implement Repeatable Field Groups in Gravity Forms (Title, Body, Image Blocks)

Hey there! Let’s tackle this repeatable sections problem you’re facing with Gravity Forms—letting clients add multiple blocks of title, body text, and images is totally achievable, and I’ve got three solid approaches to make it work for you.

1. Use the Official Gravity Forms Repeater Add-On

This is the most straightforward, supported way to get repeatable groups. Here’s how to set it up:

  • First, install and activate the Gravity Forms Repeater Add-On (it’s a paid official add-on, part of their higher-tier licenses or available as a standalone purchase).
  • In your form editor, drag the Repeater field from the fields panel into your form.
  • Now, drag your required fields (Single Line Text for the title, Paragraph Text for the body, and File Upload for the image) inside the Repeater field.
  • Configure the Repeater settings: you can set minimum/maximum allowed rows, customize the "Add Row" and "Remove Row" button text, and toggle whether rows can be reordered.
  • Save your form, and when you preview it, users will see a button to add new full blocks of title/body/image as needed.

2. Custom Code Solution (No Plugins)

If you prefer not to use an add-on, you can build this functionality with custom JavaScript and PHP hooks. Here’s a simplified example:

Step 1: Set Up Your Base Field Group

In the Gravity Forms editor, create one set of your fields (title, body, image) and assign them a shared CSS class like repeatable-group (use the "Appearance" tab in each field’s settings to add the class). Wrap this group in a HTML field with a class repeatable-container, and add a button with ID add-group to trigger adding new rows.

Step 2: Add Custom JavaScript

Add this script to your theme’s functions.php file (or a custom plugin) using the gform_enqueue_scripts hook:

add_action('gform_enqueue_scripts', 'enqueue_repeatable_group_script');
function enqueue_repeatable_group_script() {
    if (is_page('your-form-page-slug')) { // Replace with your form's page slug
        ?>
        <script>
        jQuery(document).ready(function($) {
            // Handle add group button click
            $('#add-group').on('click', function(e) {
                e.preventDefault();
                const $originalGroup = $('.repeatable-group').first().clone();
                
                // Clear all field values in the cloned group
                $originalGroup.find('input, textarea, .gfield_fileupload').val('');
                $originalGroup.find('.gfield_fileupload_remove').remove();
                
                // Update field IDs and names to avoid conflicts
                $originalGroup.find('[id^="input_"]').each(function() {
                    const oldId = $(this).attr('id');
                    const newId = oldId.replace(/(\d+)$/, match => parseInt(match) + 1);
                    $(this).attr('id', newId).attr('name', newId);
                });
                
                // Add a remove button to the cloned group
                $originalGroup.append('<button type="button" class="remove-group button">Remove Block</button>');
                
                // Append the new group to the container
                $('.repeatable-container').append($originalGroup);
            });
            
            // Handle remove group button click
            $(document).on('click', '.remove-group', function(e) {
                e.preventDefault();
                $(this).closest('.repeatable-group').remove();
            });
        });
        </script>
        <?php
    }
}

Step 3: Handle Form Submission

To ensure the repeated fields are saved correctly, use the gform_pre_submission hook to process the data. You can store the repeated groups as an array in the entry meta:

add_action('gform_pre_submission', 'process_repeatable_groups');
function process_repeatable_groups($form) {
    $repeatableData = [];
    $i = 1; // Start with the first field group
    
    while (isset($_POST['input_' . $i]) && isset($_POST['input_' . ($i+1)]) && isset($_POST['input_' . ($i+2)])) {
        $repeatableData[] = [
            'title' => $_POST['input_' . $i],
            'body' => $_POST['input_' . ($i+1)],
            'image' => $_POST['input_' . ($i+2)]
        ];
        $i += 3; // Increment by the number of fields in your group
    }
    
    // Store the data in entry meta (replace 123 with your form ID)
    if ($form['id'] == 123) {
        $_POST['gform_meta']['repeatable_blocks'] = json_encode($repeatableData);
    }
}

Note: Adjust the field IDs and increment value based on your actual form field IDs. Test thoroughly to ensure file uploads are handled correctly—you may need additional logic for image storage.

3. Free Third-Party Plugins

If you don’t want to use the official add-on or write code, search the WordPress Plugin Directory for free Gravity Forms repeat field plugins (like GF Repeat Fields or similar). These plugins add a repeatable field group option to your form editor, letting you drag fields into a repeatable container with minimal setup. Just install, activate, and configure the group settings like you would with the official add-on.

Key Tips for Success

  • Always test the form frontend and backend to ensure repeated fields save correctly.
  • For image uploads, make sure the file upload field is set to allow multiple files (if using the official Repeater) or adjust your custom code to handle file uploads for cloned fields.
  • Keep Gravity Forms and any plugins/themes updated to avoid compatibility issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:22:32