The gform_confirmation filter in Gravity Forms is used to change the confirmation message or redirect URL for a form dynamically.
Usage
Here’s a basic usage example for the gform_confirmation
filter:
add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 ); // your custom code here function custom_confirmation( $confirmation, $form, $entry, $is_ajax ) { // your custom code here return $confirmation; }
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 want displayed on the screen. To redirect the page to a 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.
More information
See Gravity Forms Docs: gform_confirmation
Examples
Multiple Forms
Dynamically change 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; }
Send Entry Data to Third-party
Post 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' ), ); $response = wp_remote_post( $post_url, array( 'body' => $body ) ); return $confirmation; }
Output Script with Confirmation Message
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 ) { if ( ! is_string( $confirmation ) ) { return $confirmation; } $confirmation .= GFCommon::get_inline_script_tag( "window.top.jQuery(document).on('gform_confirmation_loaded', function () { console.log('gform_confirmation_loaded running'); } );" ); return $confirmation; }
Note: This example also uses gform_confirmation_loaded
, so AJAX must be enabled in your form.
Open the Page or Redirect in a New Tab
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( 'gform_confirmation: 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 ) || empty( $confirmation['redirect'] ) ) { return $confirmation; } $url = esc_url_raw( $confirmation['redirect'] ); $confirmation = 'Thanks for contacting us! We will get in touch with you shortly.'; $confirmation .= GFCommon::get_inline_script_tag( "window.open('$url', '_blank');" ); return $confirmation; }, 10, 4 );
Note: $confirmation['redirect']
is used for both “page” and “redirect” type confirmations.
Customize the Message for Spam Submissions
Change the confirmation message returned when the form fails honeypot validation or is marked as spam.
add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry ) { if ( empty( $entry ) || rgar( $entry, 'status' ) === 'spam' ) { return 'The message to be displayed.'; } return $confirmation; }, 10, 3 );
Here’s how to use the form’s configured confirmation when the submission is marked as spam.
add_filter( 'gform_confirmation', 'filter_spam_confirmation', 11, 4 ); function filter_spam_confirmation( $confirmation, $form, $entry, $is_ajax ) { if ( rgar( $entry, 'status' ) !== 'spam' ) { return $confirmation; } $entry['status'] = 'active'; remove_filter( 'gform_confirmation', 'filter_spam_confirmation', 11 ); $confirmation = GFFormDisplay::handle_confirmation( $form, $entry, $is_ajax ); add_filter( 'gform_confirmation', 'filter_spam_confirmation', 11, 4 ); return $confirmation; }
Modify the Confirmation Query String
Replace the comma-separated value of a Multi Select type field in the confirmation query string with an array.
add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry ) { if ( ! is_array( $confirmation ) || empty( $confirmation['redirect'] ) ) { return $confirmation; } $field_id = 1; $field = GFAPI::get_field( $form, $field_id ); $values = $field->to_array( rgar( $entry, $field->id ) ); $confirmation['redirect'] = add_query_arg( array( 'your_param_name' => $values ), $confirmation['redirect'] ); return $confirmation; }, 10, 3 );