Search code examples
phpwordpressgravity-forms-plugin

Sending custom data to custom confirmation object - Gravity Forms


Currently I have furthered the functions of gravity forms by posting to a 3rd party using a similar code structure:

add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {

    $endpoint_url = 'https://thirdparty.com';
    $body = array(
        'first_name' => rgar( $entry, '1.3' ),
        'last_name' => rgar( $entry, '1.6' ),
        'message' => rgar( $entry, '3' ),
        );
    GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );

    $response = wp_remote_post( $endpoint_url, array( 'body' => $body ) );
    GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
}

I know there are various hookpoints that you can inject into but i'm having an issue with custom confirmations. I am able to create the filter and do a custom confirmation but I would like it to be dynamic.

When I send the data to the 3rd party a response is sent back before the filter action ends, I would like to pass that data to the custom_confirmation hook. How can I do that? Is there a way to manipulate the $form or $entry variables being passed from the after_form_submission hook?

add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
    if( $form['id'] == '101' && $form['success'] == 1 ) {
        $confirmation = array( 'redirect' => 'http://www.google.com' );
    } elseif( $form['id'] == '101' && $form['success'] == 0) {
        $confirmation = "failed";
    }
    return $confirmation;
}

Would something like what I have posted above be possible?


Solution

  • I had to perform a really ugly workaround by echoing out a script at the bottom of the after_submission hook that saves the confirmation message into variable. The variable is then passed into the browsers localSession. This is then picked up when the page is loaded and the message is displayed to the user.

    if (is_numeric($RESPONSE_VAR)) {
        $msg = "<h2>Your registration was successful</h2><br><h2>Thanks for contacting us! Check your email, and activate your account..</h2>";
        echo "<script>localSession.removeItem('confirmation'); localStorage.setItem('confirmation', '$msg');</script>";
    }else{
        $msg = "<h2>Sorry something went wrong with your application, please try again. <a href='https://URL/FORM_NAME/'>Go Back</a></h2>";
        echo "<script>localSession.removeItem('confirmation'); localStorage.setItem('confirmation', '$msg');</script>";
    }