Search code examples
wordpressgravity-forms-plugingravityforms

Create a second entry based on a checkbox or choice - Gravity Forms


I am running a contest using GF. I want to be able to create a second entry into the contest based on conditional logic of a checkbox.

For example, this is for a Breast Cancer fundraiser and there is a contest. If someone submitting their entry to the contest chooses “Yes” next to the question of “Are you a breast cancer survivor?” we want to automatically create a second entry with their information into the form.

I know its a combination of the gform_after_submission hook and the GFAPI::submit_form function, however I get lost after that


Solution

  • Here's a code snippet that will create an extra entry identical to the first if a specified checkbox is checked.

    // Update "123" to your form ID.
    add_filter( 'gform_after_submission_123', function( $entry ) {
        // Update "4" to your Checkbox field ID. Assuming this is a single checkbox field, leave the "1" as it will target the first checkbox.
        if ( $entry['4.1'] ) {
            GFAPI::add_entry( $entry );
        }
    }, 10 );
    

    I've added this to our Gravity Forms Snippet Library where it will be updated in the future if needed.