gform_after_submission

Description

The gform_after_submission action hook is executed at the end of the submission process (after form validation, notification, and entry creation). Use this hook to perform actions after the entry has been created (i.e. feed data to third party applications).

The Entry Object is available to this hook and contains all submitted values.

This hook also runs for entries which are marked as spam. It does not run for submissions which fail the honeypot validation.

Usage

Applies to all forms.

add_action( 'gform_after_submission', 'after_submission', 10, 2 );

Applies to a specific form. In this case, form id 5.

add_action( 'gform_after_submission_5', 'after_submission', 10, 2 );

Parameters

Examples

1. Update post after its creation

This example uses the gform_after_submission hook to change the post content, adding values from submitted fields, including an image field, to the post created as a result of the form submission. This is only applicable to posts created by the built-in legacy post creation feature.

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

    //getting post
    $post = get_post( $entry['post_id'] );

    //changing post content
    $post->post_content = 'Blender Version:' . rgar( $entry, '7' ) . "<br/> <img src='" . rgar( $entry, '8' ) . "'> <br/> <br/> " . rgar( $entry, '13' ) . " <br/> <img src='" . rgar( $entry, '5' ) . "'>";

    //updating post
    wp_update_post( $post );
}

2. Perform a custom action after creating posts with the Advanced Post Creation add-on

This example shows how to get the post IDs when using the Advanced Post Creation Add-On. Note: this might not work when the APC feeds are processed in the background. A better hook to use is gform_advancedpostcreation_post_after_creation.

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

	//if the Advanced Post Creation add-on is used, more than one post may be created for a form submission
	//the post ids are stored as an array in the entry meta
	$created_posts = gform_get_meta( $entry['id'], 'gravityformsadvancedpostcreation_post_id' );
	foreach ( $created_posts as $post )
	{
		$post_id = $post['post_id'];
		// Do your stuff here.
	}
}

3. Send entry data to third-party

This example demonstrates a simple approach to posting submitted entry data to a third party application.

Note: The Webhooks Add-On can be used to perform requests like this with little or no coding required. See the Triggering Webhooks On Form Submissions article.

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

4. Access the entry by looping through the form fields

This example demonstrates a simple approach to accessing the field values in the entry when you don’t know the field ids.

add_action( 'gform_after_submission', 'access_entry_via_field', 10, 2 );
function access_entry_via_field( $entry, $form ) {
	foreach ( $form['fields'] as $field ) {
		$inputs = $field->get_entry_inputs();
		if ( is_array( $inputs ) ) {
			foreach ( $inputs as $input ) {
				$value = rgar( $entry, (string) $input['id'] );
				// do something with the value
			}
		} else {
			$value = rgar( $entry, (string) $field->id );
			// do something with the value
		}
	}
}

5. Create a ticket in WSDesk

See the How to use WSDesk create ticket API with third-party forms and plugins? article for an example showing how to use the gform_after_submission hook to create WSDesk tickets.

See the Creating Tickets in WSDesk using the Webhooks Add-On article if you would prefer not to use custom code.

6. Submit entries to Tripleseat

See the Using the API Lead Form with Gravity Forms in WordPress article by Tripleseat for an example showing how to use the gform_after_submission hook.

7. Submit entries to SharpSpring

See the Integrating Gravity Forms article by SharpSpring for an example showing how to use the gform_after_submission hook, they also have a tool for generating the code snippet for your form.

8. Create an Events Calendar plugin event

This example demonstrates how the gform_after_submission hook and the tribe_create_event function can be used to create an event in the Events Calendar plugin.

add_action( 'gform_after_submission', function ( $entry ) {
	if ( ! function_exists( 'tribe_create_event' ) ) {
		return;
	}

	$start_date = rgar( $entry, '4' );
	$start_time = rgar( $entry, '5' );
	$end_date   = rgar( $entry, '6' );
	$end_time   = rgar( $entry, '7' );

	$args = array(
		'post_title'            => rgar( $entry, '1' ),
		'post_content'          => rgar( $entry, '2' ),
		'EventAllDay'           => (bool) rgar( $entry, '3.1' ),
		'EventHideFromUpcoming' => (bool) rgar( $entry, '3.2' ),
		'EventShowInCalendar'   => (bool) rgar( $entry, '3.3' ),
		'feature_event'         => (bool) rgar( $entry, '3.4' ),
		'EventStartDate'        => $start_date,
		'EventStartTime'        => $start_time ? Tribe__Date_Utils::reformat( $start_time, 'H:i:s' ) : null,
		'EventEndDate'          => $end_date,
		'EventEndTime'          => $end_time ? Tribe__Date_Utils::reformat( $end_time, 'H:i:s' ) : null,
	);

	GFCommon::log_debug( 'gform_after_submission: tribe_create_event args => ' . print_r( $args, 1 ) );
	$event_id = tribe_create_event( $args );
	GFCommon::log_debug( 'gform_after_submission: tribe_create_event result => ' . var_export( $event_id, 1 ) );
} );

9. Check entry spam status

This example shows how you can check if the entry has been marked as spam and prevent the rest of your function from running.

add_action( 'gform_after_submission', 'action_gform_after_submission_spam_check', 10, 2 );
function action_gform_after_submission_spam_check( $entry, $form ) {
	if ( rgar( $entry, 'status' ) === 'spam' ) {
		return;
	}

	// The code that you want to run for submissions which aren't spam.
}

Placement

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

Source Code

This action hook is located in GFFormDisplay::process_form() in form_display.php.