gform_confirmation

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 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.

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 ) );

	$response = wp_remote_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.

// Change 3 in the following line to your form id number or remove _3 to run it for all forms.
add_filter( 'gform_confirmation_3', 'gf_confirmation_add_script', 10, 4 );
function gf_confirmation_add_script( $confirmation, $form, $entry, $ajax ) {
	GFCommon::log_debug( __METHOD__ . '(): Running...' );
	if ( ! is_string( $confirmation ) ) {
		return $confirmation;
	}

	// Replace the content of this variable with your script code. 
	$script = "window.top.jQuery(document).on('gform_confirmation_loaded', function () { console.log('gform_confirmation_loaded running'); } );";

	$confirmation .= GFCommon::get_inline_script_tag( $script );
	GFCommon::log_debug( __METHOD__ . '(): Custom Confirmation: ' . print_r( $confirmation, true ) );

	return $confirmation;
}

Note: This example also uses 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.

Note that it’s up to the browser to allow the new window/tab from being opened or not. We can’t do anything to bypass browser restrictions.

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'] );
	GFCommon::log_debug( __METHOD__ . '(): Redirect to URL: ' . $url );
	$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.

5. Customize the message for spam submissions

The following example shows how you can 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;
}, 11, 3 );

The following example shows 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;
}

6. Modify the confirmation query string

This example shows how you can 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;
	}

	/** @var GF_Field_MultiSelect $field */
	$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;
}, 11, 3 );

7. Redirect to a Post created using the Advanced Post Creation add-on

This example shows how you can redirect the user to a post created after submission, you need to change 1937 in the filter name to your form id number.

add_filter( 'gform_confirmation_1937', 'gf_redirect_to_apc_post', 10, 4 );
function gf_redirect_to_apc_post( $confirmation, $form, $entry, $ajax ) {
	GFCommon::log_debug( __METHOD__ . '(): Running...' );

	$created_posts = gform_get_meta( $entry['id'], 'gravityformsadvancedpostcreation_post_id' );
	foreach ( $created_posts as $post ) {
		$redirect_url = get_permalink( $post['post_id'] );
	}

	if ( ! empty( $redirect_url ) ) {
		$confirmation = array( 'redirect' => $redirect_url );
		GFCommon::log_debug( __METHOD__ . '(): Redirect URL set to => ' . $redirect_url );
	}

	return $confirmation;
}

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFFormDisplay::handle_confirmation() in form_display.php.