DescriptionUsageParametersExamples1. Multiple forms2. Send entry data to third-party3. Output script with confirmation message4. Open the page or redirect in a new tab5. Customize the message for spam submissions6. Modify the confirmation query stringPlacementSource Code
Description
This filter can be used to dynamically change the confirmation message or redirect URL for a form.
Usage
add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 );
You can also specify this per form by adding the form id after the hook name.
add_filter( 'gform_confirmation_6', 'custom_confirmation', 10, 4 );
Parameters
$confirmation string | array
The confirmation message/array to be filtered. For a simple confirmation message, set this variable to the message you would like displayed in the screen. To redirect the page to an URL, set this variable to an array in the following format:
$confirmation = array( 'redirect' => 'http://www.google.com' );
$form Form Object
The current form.
$entry Entry Object
The current entry.
$is_ajax bool
Specifies if this form is configured to be submitted via AJAX.
Examples
1. Multiple forms
This example dynamically changes the confirmation message for form 102 and set form 101 to redirect to www.google.com.
add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
if( $form['id'] == '101' ) {
$confirmation = array( 'redirect' => 'http://www.google.com' );
} elseif( $form['id'] == '102' ) {
$confirmation = "Thanks for contacting us. We will get in touch with you soon";
}
return $confirmation;
}
2. Send entry data to third-party
This example demonstrates a simple approach to posting submitted entry data to a third party application. You could even assign some part of the response to the $confirmation message.
add_filter( 'gform_confirmation', 'post_to_third_party', 10, 3 );
function post_to_third_party( $confirmation, $form, $entry ) {
$post_url = 'http://thirdparty.com';
$body = array(
'first_name' => rgar( $entry, '1.3' ),
'last_name' => rgar( $entry, '1.6' ),
'message' => rgar( $entry, '3' ),
);
GFCommon::log_debug( 'gform_confirmation: body => ' . print_r( $body, true ) );
$request = new WP_Http();
$response = $request->post( $post_url, array( 'body' => $body ) );
GFCommon::log_debug( 'gform_confirmation: response => ' . print_r( $response, true ) );
return $confirmation;
}
3. Output script with confirmation message
The following example shows how you can output a script along with the form confirmation message.
add_filter( 'gform_confirmation_3', 'custom_confirmation_message', 10, 4 );
function custom_confirmation_message( $confirmation, $form, $entry, $ajax ) {
$confirmation .= "";
return $confirmation;
}
Note: This example uses also gform_confirmation_loaded, so AJAX must be enabled in your form.
4. Open the page or redirect in a new tab
The following example shows how you can open the 「page」 or 「redirect」 type confirmation in a new tab. You need to update the $forms array with the id number of the form(s) that you want to target.
add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry, $ajax ) {
GFCommon::log_debug( __METHOD__ . '(): running.' );
$forms = array( 3, 6, 7 ); // Target forms with id 3, 6 and 7. Change this to fit your needs.
if ( ! in_array( $form['id'], $forms ) ) {
return $confirmation;
}
if ( isset( $confirmation['redirect'] ) ) {
$url = esc_url_raw( $confirmation['redirect'] );
GFCommon::log_debug( __METHOD__ . '(): Redirect to URL: ' . $url );
$confirmation = 'Thanks for contacting us! We will get in touch with you shortly.';
$confirmation .= "
Parameters
event JS Object | object
The Javascript event object.
formId integer
The ID of the form submitted.
Examples
This event is very useful when the popular font substitution script Cufon is being applied to a Gravity Form which uses the AJAX form submission functionality. The snippet below provides an example usage of the gform_confirmation_loaded event to refresh the Cufon font substitution when the confirmation page is loaded.
1234567
Here is another example where the form ID parameter is used to run different code depending on which form』s confirmation is being loaded.
12345678910111213
Source Code
This filter is located in form_display.php.